Shell Scripting Flashcards
What should the first line be in a bash script?
#!/bin/bash *the shebang as the first line in the script tells bash what scripting language is being used
How do you run a script without specifying a path, and calling like a native command?
Move script to one of the directories contained within the $PATH env variable
i.e /home//bin
How do you evaluate (run) a command in quotation marks?
$()
i. e echo “$(ls)”
* `` - legacy: i.e ls
will substitute value of ls
How do you make a script executable?
chmod +x scriptname
How do you write an if statement in bash?
if [ ]; then
>command<
fi
How do you write an if else statement in bash?
if [ ]; then >command< else >command< fi
How do you write a for loop in bash?
for i in {1..10}
do
echo “$i”
done
In bash, what is the stored variable containing the number of arguments the script was run with?
$#
In bash, what is the stored variable that contains the scripts own filename?
$0
In bash, what is the stored variable containing script arguments?
$1..$n
What is an alternative way to write a variable within quotes?
${} i.e var=tree echo "${var}s" *useful for string seperation "$vars" won't work
How do you write a function in a bash script?
function multiply() { echo 5 * $1 } *call function multiply 2
How do you run a command sequentially after another?
;
i.e pwd; ls
How do you run a command sequentially only if the first was successful?
&&
i.e ls -lh file && cat file
How do you run a command sequentially only if the first was unsuccessful?
||
i.e ls nonexistant || touch nonexistant