Building Bash Scripts
A Bash script can contain many of these commands and often makes use of variables, arguments and procedural flow-control logic. Of course, you can enter these things line by line at the command prompt, but the whole point of having scripts is that you don't have to do that. Your script will start with an interpreter directive that's sometimes called a hashbang or shebang. That nickname comes from the first two characters of the line - a number or pound sign or hash sign, and an exclamation mark which is sometimes called bang.
Your script will start with an interpreter directive that's sometimes called a hashbang or shebang. That nickname comes from the first two characters of the line - a number or pound sign or hash sign, and an exclamation mark which is sometimes called bang.
Displaying text with echo:
One of the most basic commands that you'll use in a batch script is the echo command. Echo prints out information normally to the standard output. They can be directed elsewhere. The syntax for the echo command is pretty simple. You just type echo, followed by whatever you want to output. However, there are a couple different things you should keep in mind about quote marks.
Below script echo.sh consists of three conditions: "no quotes", "single quotes" and "double quotes".
#!/bin/bash
# This is a basic bash script
greeting="hello"
#no quotes
echo $greeting,world \(planet\)!
#single quotes
echo '$greeting,world (planet)!'
#double quotes
echo "$greeting,world (planet)!"
No quotes:
If you write a statement with no quotes, Bash goes along and interprets things as it finds them and you will get an error here, because if you need to use special characters, you have to escape them with a backslash.
Single quotes:
single quotes or strong quotes, where nothing inside of the quotes gets interpreted. So everything comes out literally, even if you put a variable inside, you get back literally what you typed, not the variable.
Double quotes
finally there's a middle way, using double quotes. If you want to have Bash display something literally inside of double quotes, you could escape it with backslash
If you want to add empty lines to your output, just type echo with nothing after it.
No comments:
Post a Comment