2.1 Running Commands
In Bash, scripts will run statements similarly to how you would run them if you were to do so manually. Commands and statements are ran in sequence and are interpreted by the individual runtimes of each command. These can be constructed on any variety of programming language, or even written directly in Bash as a function.
Some common commands you will likely run across when writing Bash scripts can include the following:
Command | Description |
---|---|
echo | Prints text to the user |
ls | Lists files/directories in the current working directory |
cat | Reads input from the provided file and prints it to the standard output |
pwd | Prints the current working directory of the shell (where we are located at) |
cp | Copies a file or folder to a given directory |
Command Reference
Below are some examples of how the above commands can be used. Any commands that your system has available can be used in your bash scripts.
Some snippets below include a more advanced concept called "piping" to demonstrate their functionality. You can check this out later or view notes for this feature here: 3.2 Piping & Redirects
echo
# Prints current shell value using global SHELL variable
echo Hello $SHELL!
ls
# Prints all files and folders in current directory
ls -la ~/
cat
# Save content to temp file
echo Hello world from the inside of a file! > temp.txt
# Print file content to standard output
cat temp.txt
pwd
# Prints the current working directory of the script
pwd
cp
# Save content to temp file
echo Hello world from the inside of a file! > temp.txt
# Copy from temp to temp2
cp temp.txt temp2.txt
# Print temp2
cat temp2.txt