A Unix shell provides a text-based user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and it is used by the operating system to control the execution of the system using shell scripts.

This tutorial will walk you through some Unix shell commands that will be useful to know as you progress on your coding journey. This guide is not an exhaustive list.


table of contents

how to execute a unix shell command

unix shell shortcuts

commands

resources and references


how to execute a unix shell command

A Unix shell command can be executed through a CLI application running a Unix shell instance, or it can be executed as part of a Unix shell script.

Once you have opened a CLI application, type the command into the console and press ENTER or RETURN to execute it. Some commands will print text as a part of their execution, and some will not. An error message will usually be printed if something has gone wrong during the program execution or if the command and its arguments were not structured properly. Additional information about CLI applications can be found in the software for unix guide.

Additional information about creating, writing, and executing shell scripts can be found in the unix shell scripts guide.


unix shell shortcuts

Use the TAB key to autocomplete file, directory, and command names.

Use the UP arrow key to scroll through previous commands.

Use the DOWN arrow key to scroll forward through previous commands.


commands

pwd

The pwd command prints the absolute file path of your current location in the shell. Your current location will correspond to some folder in your computer’s file system.

whoami

The whoami command prints your username. This command can be used to check your username when you are connected to a remote server.

date

The date command prints the current date and time.

echo STRING_HERE

The echo command will print the given string.

[!TIP] Fun fact: Want to make your computer beep? Try the following: echo -e "\a"

echo $VARIABLE_NAME_HERE

When given an environment variable name, such as $HOME or $PATH, the echo command will print the value of the given variable.

examples

echo "Hello, World!"
echo $HOME

clear the shell window

clear

The clear command clears the shell window.

list the directory contents

ls

The ls command prints all the visible files and folders in our working directory.

ls -l

When we add the -l flag to the ls command, the visible files and folders will be printed in a long list format.

ls -a

When we add the -a flag to the ls command, the command will list all the files and folders in our working directory, including hidden files and folders.

Hidden files and folders will always have a name that begins with . (e.g. .bash_profile).

ls -al

When we add both the -a and -l flags to the ls command, the command will list all files in our working directory, including hidden files, in a long list format.

ls DIRECTORY_PATH_HERE

We can use the ls command to print the contents of a different directory by providing the absolute or relative path to the directory.

Additional information about paths can be found in the absolute and relative paths guide.

change the working directory

cd DIRECTORY_PATH_HERE

The cd command will change the working directory to the directory with the given absolute or relative path.

Additional information about paths can be found in the absolute and relative paths guide.

create a file

touch FILENAME_HERE

The touch command creates a file.

touch FILE_PATH_HERE

When executing the touch command, the filename argument can also be a relative or absolute path to the file you are creating.

Additional information about paths can be found in the absolute and relative paths guide.

examples

touch my-text-file.txt
touch ./my-directory/MyJavaClass.java

create a directory (folder)

mkdir DIRECTORY_PATH_HERE

The mkdir command creates a folder, also known as a directory. The directory path argument can be an absolute path or a relative path.

Additional information about paths can be found in the absolute and relative paths guide.

change file permissions

chmod PERMISSIONS_HERE FILE_PATH_HERE

The chmod command updates the permissions of the given file using the given permissions.

Permissions can be provided in symbolic mode or octal mode.

A guide on file permissions is coming soon!

examples

touch my-text-file.txt
chmod 444 my-text-file.txt
chmod ugo+w my-text-file.txt

set the value of an environment variable

export VARIABLE_NAME_HERE=VARIABLE_VALUE_HERE

The export command will set the given variable to the given value. When executed in a Unix shell, this change will only be active for the current shell session.

If an environment variable needs to have some value in every shell session, it is recommended to set that variable in the appropriate shell profile file or shell RC file.

Additional information about shell profile files and RC files can be found in the shell profile and rc files guide.

To confirm the variable has been set properly, you can print its value using the echo command.

echo $VARIABLE_NAME_HERE

examples

export SOME_VARIABLE="some value"
echo $SOME_VARIABLE
which COMMAND_NAME_HERE

The which command prints the absolute path to the location of the given command, provided as an argument.

examples

which ruby
which python
where COMMAND_NAME_HERE
which -a COMMAND_NAME_HERE

The where command and the which -a command are equivalent; they print all paths to the given command.

The where command gives you all locations of the given command; the which command tells you which one will be used when you execute that command.

examples

where ruby
which -a ruby

start a Bash shell instance

bash

The bash command initializes an instance of the Bash shell inside the current shell instance.

start a Z shell instance

zsh

The zsh command initializes an instance of the Z shell inside of the current shell instance.

exit the current shell instance

exit

The exit command can be used to exit the current shell instance.


resources and references

AWS - What is a CLI?

Wikipedia - Unix shell

Wikipedia - Bash (Unix shell)

Wikipedia - Z shell

explainshell

MULTACOM: Understanding File Permissions