W1: Linux - Bash Language Flashcards
- Keywords: bash
GNU Bourne-Again SHell
sh-compatible language interpreter, executes commands read from an input .sh filew
- Keywords: Global Variables
Global variables can be used by all Bash scripts on your system
- Keywords: Local Variables
Local Variables can only be used within the script (or shell) in whech they’re defined
- Keywords: printenv
prints environment variables, which are the values kept in the machine that are in use by programs running in terminals/subshells.
ALSO prints previously specified environment variables in the shell.
aka attributes with a title and a value.
- Keywords: Index/associative arrays
array: stores multiple data.
with index and the value of each array element accessed by its corresponding index value.
- Keywords: for, while, until loops
N=10
for i in $(seq 1 $N); do
echo “$i-th run”
done
i=0
while [ $i -lt 100 ]; do
i=expr $i + 1
echo $i
done
i=0
until [ $i -ge 100 ]; do
i=expr $i + 1
echo $i
done
- Keywords: break,
break 1
terminates current loop, passes program control to the command that follows the terminated loop.
- Keywords: continue
continue 1
skips the remaining commands in the current loop iteration, passes program control to the next loop iteration.
- Keywords: select
https://linuxhint.com/bash_select_command/
used in scripts for menu creation
#!/bin/bash
echo “Which Operating System do you like?”
Operating system names are used here as a data source
select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP
do
case $os in
# Two case values are declared here for matching
“Ubuntu”|”LinuxMint”)
echo “I also use $os.”
;;
Three case values are declared here for matching
“Windows8” | “Windows7” | “WindowsXP”)
echo “Why don’t you try Linux?”
;;
Matching with invalid data
*)
echo “Invalid entry.”
break
;;
esac
done
- Keywords: PS3
PS1: primary prompt string
PS2: secondary prompt string
PS3: this parameter’s value is used as prompt for the select command
PS4:
- Keywords: unset
unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks
https://unix.stackexchange.com/questions/382391/what-does-unset-do
- Keywords: if statement
!/bin/bash
# Basic if statement if [ 1000 > 100 ] then echo Hey that\'s a large number. pwd fi date
- Keywords: case statement
!/bin/bash
simplifies complex conditions with multiple different choices. easier to read and maintain than nested if.
echo “Which color do you like best?”
echo “1 - Blue”
echo “2 - Red”
echo “3 - Yellow”
echo “4 - Green”
echo “5 - Orange”
read color;
case $color in
1) echo “Blue is a primary color.”;;
2) echo “Red is a primary color.”;;
3) echo “Yellow is a primary color.”;;
4) echo “Green is a secondary color.”;;
5) echo “Orange is a secondary color.”;;
*) echo “This color is not available. Please choose a different one.”;;
esac
- Keywords: function
!/bin/bash
BashFunction () {
echo Hello, my linux friends
}
BashFunction
- Keywords: /dev/stdin
standard input stream file. what’s input there is what’s input to the console,
what’s input to stdout is console output