Bash enable you to create variables and set initial values. Later you can extract or modify the value of a variable using specific operators and expressions or commands. The most simple way to define a variable is to assign initial value to an identifier that becomes the variable name.
Identifier name can't start with a number or special character. It usually start with a letter and can be followed by more letters, digits or underscore. You can't use a dash or a space for identifiers. Identifiers are case sensitive.
Initial value is given by using assign sign "=". Some other operators like "+=" for examples will cause a new variable to be created and assigned the default zero value. This can be an empty string or 0 depending on the context.
#!/bin/bash var=1 echo $var str="hello world" echo $str new+=1 echo $new
Bash has support for data literals. Numeric data literals are by defaul parsed as decimal integers. If a literal look like a number it can be parsed and interpreted as a number depending on the context.
A sequence of symbols that are not separated by paranthesis or spaces can be considered string even if is not enclosed in double quotes. That may be confusing, therefore is a good practice in Bash to use double quotes to contain a string.
In next example we display a sequence of commands given to the Bash interpreter in CLI (Command Line Interface). You can do this for quick testing or issuing commands.
>bash bash-3.2$ bash-3.2$ xp=1+2+3 bash-3.2$ echo $xp 1+2+3 bash-3.2$
Note: In previous example we may expect for the expresion to be evaluated and the result to be numeric but Bash has other ideas. It will create a string that is containing the expression as given.
Quoted strings can support spaces and paranthesis. Also, there is an escape convention. A string can contain special characters escaped like in C using backslash symbol: "\"
~>bash $ echo "This is \n a long string \n with new lines" This is a long string with new lines $exit ~>
Note: In this example, $ is the Bash prompt. Bash is waiting for a new command. You can stop CLI by typing command: exit. Symbol ~ represent home folder and is sometimes present in the command line prompt.
You can't test directly for type of a variable but you can test if a variable is empty using a conditional expression. We have not yet learn how to make conditionals.
Expression [ -z "$var" ] represents a conditional expression that is evaluated to true if variable var is empty or unset. You will understand this example later after you learn conditionals and logical expressions.
>bash $ [ -z "$var" ] && echo "var is undefined" $ var is undefined $ var=0 $ [[! -z "$var" ]] && echo "var is defined" var is defined $
Every Bash session has a global scope. In this scope you can define environment variables. This is an array that contains name=value pairs. There are specific commands to add variables to this environment and inspect values. Usually these variables are defined using capitall letters.
You can investigate the environment variales using command "env" or "set". To extract value of a single variable you can use command "echo" and prefix variable name with symbol "$". This may be confusing for beginners, because the Bash prompt uses the same symbol. Do not confuse the two.
You can add a new variable to the environment by using command "export". After export the variable is available to echo command so you can display it's value. This command can also export functions not only variables!
>bash $ export TEST="bash is cool" $ echo $TEST $ bash is cool $
Strings can be concatenated in Bash if they are side by side with other strings but not separated by a space. Any adiacent symbol except parathesis and spaces will be concatenated to the string. Strings can be modified using "+=" operator.
>bash $ echo "hello ""world" hello world $ echo a" test" a test $
Strings can be combined with variables. For this we use symbol "$" in front of variable, to access its value. The value is inserted in the string, replacing the variable placeholder. The string formating works with command "echo" and "printf" but also in expressions.
>bash $ str="world" $ echo "hello &str" hello world $ x = 10 $ echo "x = $x" x = 10 $
Long strings are possible on multiple lines. The spaces in long strings are preserved as is. So you can write a sequence of code and the indentation is preserved. You can also use the end of line continuation inside a string but is not necesary. The continuation symbol is backslash. "\"
I have create this example named "longstr.sh" on replit.com website.
#!/bin/bash echo \ "this is a long string"
>bash longstr.sh this is a long string $
Read next: Arithmetic Expressions