Key Concepts

Review core concepts you need to learn to master this subject

Bash Script Arguments

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

Arguments can be added to a bash script after the the script’s name. Once provided they can be accessed by using $(position in the argument list). For example, the first argument can be accessed with $1, the second with $2, the third with $3, etc.

Bash script variables

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

Variables in a bash script can be set using the = sign, and accessed using $.

bash script read keyword

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

The read command can be used to prompt the user for input. It will continue to read user input until the Enter key is pressed.

Some prompt text can also be specified using -p with the read command.

Bash Shebang

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

Bash script files start with #!/bin/bash. This special line tells the computer to use bash as the intepreter.

Bash Aliases

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

Aliases can be created using the keyword alias. They are used to create shorter commands for calling bash scripts. They can also be used to call bash scripts with certain arguments.

Bash Scripts

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

Reusuable sets of bash terminal commands can be created using bash scripts. Bash scripts can run any command that can be run in a terminal.

Bash script comparison operators

#!/bin/bash # For a script invoked by saycolors red green blue # echoes red echo $1 # echoes green echo $2 # echoes blue echo $3

In bash scripting, strings are compared using the == (Equal) and != (Not equal) operators.

Learn Bash Scripting
Lesson 1 of 1
  1. 1
    Bash (or shell) scripting is a great way to automate repetitive tasks and can save you a ton of time as a developer. Bash scripts execute within a Bash shell interpreter terminal. Any command you c…
  2. 2
    Within bash scripts (or the terminal for that matter), variables are declared by setting the variable name equal to another value. For example, to set the variable greeting to “Hello”, you would us…
  3. 3
    When bash scripting, you can use conditionals to control which set of commands within the script run. Use if to start the conditional, followed by the condition in square brackets ([ ]). then begin…
  4. 4
    There are 3 different ways to loop within a bash script: for, while and until. A for loop is used to iterate through a list and execute an action at each step. For example, if we had a list of wor…
  5. 5
    To make bash scripts more useful, we need to be able to access data external to the bash script file itself. The first way to do this is by prompting the user for input. For this, we use the read s…
  6. 6
    You can set up aliases for your bash scripts within your .bashrc or .bash_profile file to allow calling your scripts without the full filename. For example, if we have our saycolors.sh script, we c…
  7. 7
    Take a minute to review what you’ve learned about bash scripting. - Any command that can be run in the terminal can be run in a bash script. - Variables are assigned using an equals sign with no sp…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo