#!/bin/bash
#! <- shebang
/bin/bash <- use bash shell
- Don't use space between var and value, it won't work.
- A variable can only hold one value, so a string with spaces must be quoted.
- The shell does not care about types of variables; they may store strings, integers/float/etc.
- Special characters must be properly escaped to avoid interpretation by the shell.
- Variable in bash do not have to be declared first.
- If you try to read an undeclared variable, the result is the empty string.
- Curly brackets around a variable avoid confusion.
MESSAGE="Hello World"
SHORT_MESSAGE=hi
NUMBER=1
PI=3.142
NAME="Darin"
echo $NAME
echo "$NAME"
echo "${NAME}!"
Special variable
$# = number of parameters the script was called with.
$0 = basename of the program as it was called.
$1 .. $9 = the first 9 additional parameters the script was called with.
$@ = all parameters
$* = all parameters, but does not preserve any whitespace and quoting
$? = the exit value of the last run command.
$?? = is the PID (Process IDentifier) of the currently running shel
$! = PID of the last run background process
- Export is one of the bash shell BUILTINS commands.
- Export the variable for it to be inherited by another program.
- For example :
$ NAME="Darin"
$ echo $NAME
Darin
$ echo $NAME
$
$ export NAME
$ echo $NAME
Darin
- Some characters like (", $, `, and \) are still interpreted by the shell, even when they're in double quotes.
- The backslash (\) character is used to mark these special characters so that they are not interpreted by the shell.
display hello "world"
echo "Hello \"World\""
- For usually used as looping with a specific value.
- For usually used as looping continously (while true).
for i in condition
do
something
done
STRING=hello
while [ "$STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read STRING
echo "You typed: $STRING"
done
# : represent true.
while :
do
echo "Please type something in (^C to quit)"
read STRING
echo "You typed: $STRING"
done
- Test or represent as [ is a shell builtin
- semicolon (;) to join two lines together.
- The backslash (\) serves a similar, but opposite purpose:
- It tells the shell that this is not the end of the line
- But that the following line should be treated as part of the current line.
- -a, -e (both meaning "file exists")
- -S (file is a Socket)
- -nt (file is newer than)
- -ot (file is older than)
- -ef (paths refer to the same file)
- -O (file is owned my user),
if [ ... ]
then
# if-code
else
# else-code
fi
if [ something ]; then
echo "Something"
elif [ something_else ]; then
echo "Something else"
else
echo "None of the above"
fi
#!/bin/sh
echo "Please talk to me ..."
while :
do
read INPUT_STRING
case $INPUT_STRING in
hello)
echo "Hello yourself!"
;;
bye)
echo "See you again!"
break
;;
*)
echo "Sorry, I don't understand"
;;
esac
done
echo
echo "That's all folks!"
- The backtick (`)is also often associated with external commands.
- Backtick can be used to store long commands output to variable.
LISTUSER=`cat /etc/passwd | awk -F : '{print $1}'`
echo $LISTUSER
- When writing a suite of scripts, it is often easier to write a "library" of useful functions \
- And source that file at the start of the other scripts which use the functions.
- A function may return a value in one of four different ways:
1. Change the state of a variable or variables
2. Use the exit command to end the shell script
3. Use the return command to end the function, and return the supplied value to the calling section of the shell script
4. echo output to stdout, which will be caught by the caller just as c=`expr $a + $b` is caught
function_name () {
commands
}
#!/bin/bash
hello_world () {
echo "hello, world"
}
hello_world #call and execute function
https://linuxize.com/post/bash-functions/