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
.
./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.
$ 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
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
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.
$ cat hello2 #!/bin/bash echo "Hello $myvar" $ myvar="World" $ ./hello2 Hello $ myvar=everyone ./hello2 Hello everyone $ echo $myvar World $ export myvar $ ./hello2 Hello World
$myvar
$myvar
expansion produces NULL$myvar
now expands to the assignment on the command line.$myvar
expands to value, since it is exportedecho
Commandecho
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
.
echo -n
Commandecho
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
.
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 3You 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
source
command can be abbreviated
as just a .
with a space after it:
source file . file
echo
command.
echo 'line $one line \"$two\" line three' > file
will produce the three line file:
line $one line \"$two\" line three
\"
, \$
, \`
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:
line 1 line "2" line three
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.