Bash: Input/Output - notes

Notes

Executable bash script in a file

To create an executable script, put bash commands into a file that begins with the line #!/bin/bash, and make the file executable. This file can now be used as a bash command with arguments and I/O redirects. The script will execute in a new shell, and the commands in the file will have access to Exported variable values and positional parameters. When the shell completes, or an exit command is executed, the new shell terminates and the process continues in the old shell with same environment as before. This is to protect the old shell from unintended side affects. The new shell can communicate to the old shell by reading and writing to the streams on the original command, or by reading and writing to files by filenames. The new shell can also set an exit status to be used in old shell with commands such as if or while.

When testing the command you should refer to using the current directory such as ./hello. This way you are explicitly load the file and not relying on the shell to find the command in your path. When you want to install an executable script for general use you should copy it to the your ~/bin directory. This directory is in your default path.

Consider the example
$ cat hello1
#!/bin/bash

echo "Hello World"
$ hello1 >save
-bash: hello1: command not found
$ ./hello1 >save
-bash: ./hello1: Permission denied
$ chmod +x ./hello1
$ ./hello1 >save
$ cat save
Hello World

cat hello1
Displays the three line file.
hello1 >save
The command is not found, since your path does not include the current directory.
./hello1 >save
Permission to execute the command is denied.
chmod +x ./hello1
Set the mod on the file to allow executution.
./hello1 >save
Success - No error message.
cat save
Display the saved output of the command.

positional parameters

Arguments on the the command line, excluding redirects, are positional parameters, and are numbers $0, $1, $2, ... . The value of $0 is the command name and $1, $2, ... are the arguments to the command. The value of $@ is all the arguments, while $# is the number of arguments. The shift shell command will shift off the $1, which makes it easy to loop through all positional parameters in the while loop:

while [ $# -gt 0 ]; do ... Here $1 is current and $2 is the next ($0 is unchanged) shift done

Exported variables

a bash script begins with a new environment. This includes exported variables from the parent shell and any assignments on the command line. The exported variables are the ones exported to the parent script as well as any new variables listed on the export command.

Consider the example:
$ cat hello2
#!/bin/bash

echo "Hello $myvar"
$ myvar="World"
$ ./hello2
Hello 
$ myvar=everyone ./hello2
Hello everyone
$ echo $myvar
World
$ export myvar
$ ./hello2
Hello World

cat hello2
Displays the three line file.
myvar="World"
Assigns the variable to be used by the parameter expansion $myvar
./hello2
The $myvar expansion produces NULL
myvar=everyone ./hello2
The $myvar now expands to the assignment on the command line.
echo $myvar
The value of myvar is unchanged when continuing in the original shell.
export myvar
Exported variables are put in the environment of all new shells.
./hello2
The $myvar expands to value, since it is exported

Note on echo Command

The echo shell command will print the values of the arguments to STDOUT. If there are multiple arguments they will appear in the output with one space separating them. The output can be saved in a file with a command redirection of the form >filename.

Note on echo -n Command

The echo command with the -n shell print the values of the arguments to STDOUT, without a terminating CR. In a terminal session this will cause the output to be on the same line as the prompt.
$ echo foo
foo
$ echo -n foo
foo$ 
Inside a script it is useful to always using echo -n to only include explicitly supplied line endings in the quoted string. Inside a double quoted string a line ending the backslash \ will count as supplied line ending. Thus a sequence for echos of the form:

echo -n "\ line 1 line 2 line 3 "

will output three lines.

Lines between the echo -n "\ and " will appear as multiple lines. On outputted lines values ov variables will be inserted using the dollar $myvar and out of command wil be inserted with the backtick quote `mycommand`. This means there are exactly four special characters, which must be quoted to appear in output \" \$ \' \\.

note: This is a near equivalent to the here document redirection on the cat command. Start cat <<EOT and end with EOT.

Note on Variable Assignment

The most basic operation in shell program is assigning a value to a variable for later expansion. Assignment are of the form:

myvar=value

Make sure there is no spaces around the = and you must quote the value if there are any spaces. To expand the value into a later statement use the form ${myvar}. The brackets are optional if it is clear where the variable name ends. For example

my=1 myvar=" 2 3" echo $myvar echo ${my}var echo $my var echo "$myvar"

$ my=1
$ myvar=" 2    3"
$ echo $myvar
2 3
$ echo ${my}var
1var
$ echo $my var
1 var
$ echo "$myvar"
 2    3
You should always enclose a variable in double quotes when you expand it as a token in a command. That way it will be one argument. Consider the test command:
$ test $myvar = " 2    3" && echo equal
-bash: test: too many arguments
$ test "$myvar" = " 2    3" && echo equal
equal

Note on Source Command

The source command will read and execute commands from a file. All commands in the file will have access to all your variables and any assignments made in the file will be available when the sourcing in complete. This is a global name space for the variables. This is very much different from executing a shell script. Only exported variables are available, and all changes are only in the sub-shell. The source command can be abbreviated as just a . with a space after it:

source file . file

Note on Single Quotes

Inside a single quoted string no characters have special meaning and will all appear in the string. This includes the new line character, return, which will allows a multi-line output with one echo command.

echo 'line $one line \"$two\" line three' > file

will produce the three line file:

file:
line $one
line \"$two\"
line  three

Note on Double Quotes

Inside a double quoted string their are 4 characters with special meaning and for them to appear in the string. The quotes \", \$, \` and \\ all expand as one character, a single \ at the end of a line, escaped return, is expanded as null, which means the lines are joined together.

one=1 two=2 echo "\ line $one line \"$two\" line three" > file

will produce the three line file:

file:
line 1
line "2"
line  three

Note on Bash Functions

A bash function is a block of commands which is invoked in your shell by just using the name. The commands are execute in the environment of the shell.

The function is defined in the script file, or on the command line, with:

function name { 
  COMMANDS 
}
If the name includes parenthesis then the function keyword is optional. Inside the block of commands the positional parameters, $1, $2 ... and the entire list $@ refer to the parameters on the function statement, and not the parameters on the the shell script. Otherwise the shell variables are available for paramater expansion, and they can be set. This is a global name space. You can define local variables, which will not be seen by the main script, with the local command.

IT Help Center
University of Delaware
Last updated: August 11, 2010
Copyright © 2010 University of Delaware