Bash: A User Interface
A shell is a user interface that runs an interpreter. The interpreter is a program that accepts user input, determines how to execute that input, converts it into executable statements, and executes them. The environment's role is to capture definitions and previous commands from the current session, allowing users to easily recall them and simplify their interaction with the system.
The development of shells has evolved over time, with major milestones including the Thompson Shell (1971), Mashey Shell, Bourne Shell (1977), C-shell (1978), and TC-Shell (1983). The Bourne Again Shell (Bash), released in 1989, is one of the most popular shells used today.
Bash - 'Bourne Again SHell'
(Git Bash - for Windows interaction with Git)
The grammar of a shell allows combining existing tools into powerful pipelines and handling large volumes of data automatically. Writing a sequence of commands in a script improves the reproducibility of workflows.
Terminal Basics
$ is the prompt for typing, followed by a blinking text cursor.
Ctrl + Alt + T # Launch terminal
Ctrl + Shift + C # Copy from terminal
Ctrl + Shift + V # Paste in terminal
clear # Clear the terminalsu- Switch user to super usersudo- Execute a command with super user permissionsapt- Package installer for Debian-based systems- Switch between users using
su(switch user) orsudo suto switch to root.exitto end the root session.
sudo apt update # Update package index
sudo apt upgrade # Upgrade installed packagesShell Command Prompt
The shell prompt shows essential information about the user, system, and their current state. A typical prompt looks like this:
sujith @sujith-Latitude-7490 ~ $~ represents the home directory, $ indicates a normal user. When logged in as a root user, the prompt changes to #. The # symbol signifies that the user has root (administrator) privileges.:
root @sujith-Latitude-7490 :home/sujith #General Syntax of a Shell Command
The general structure for a Linux command is:
command [option(s)] [parameter(s)]$ ls -F /$is the promptlsis the command-Fis the option/flag/is the parameter (the root directory)
Commands, Options, and Parameters
A command does not always require arguments or options;
it can be called with multiple options and arguments (collectively referred to as parameters).
Options change the command's behavior:
Options typically follow a hyphen -.
Short options start with a single dash (-), e.g., -r, -a.
Long options start with double dashes (--), e.g., --reverse, --all.
Some options may act differently in different commands or same way across multiple commands.
-f / --forcefor force and-i / --interactiveis incp, mv, rm-r / --recursiveperform recursive operation forcprm-afor all and-h / --helpare common in most commands
Parameters or Arguments are often required or optional, depending on the command. These specify the target of the command, such as file names or directories.
Command options arguments have to be separated by spaces or tabs for the system to interpret them as words. Omitting spaces causes confusion about commands, options, and arguments.
(e.g., ls-F searches for a command called ls-F, which does not exist)
A contiguous string of spaces and tabs together form whitespace. Systems permits the use of one white space which can be many tabs and spaces. Which gets compressed to single space.
Case sensitivity matters:
ls -sdisplays the size of files.ls -Ssorts files by size.
Common Shell Commands
Some common shell commands and their functions include:
cd: Change the directoryls: List files in the current directorypwd: Display the current directory pathvioremacs: Text editors for editing fileswho: Show who is logged inwhoami: Display the current usernamehostname: Display the computer’s hostnameuname: Show system information (e.g.,uname -ofor the operating system)arch: Show the computer’s architectureecho: Print text or values to the screen (e.g.,echo $SHELLto display the shell being used)bash: Start a new Bash session within the current sessionexit: Exit the current Bash session (If it is the outermost one, it will close the window)passwd: Change the user passwordps: Report a snapshot of the current processes.stty: Change and prints Terminal Settings (-ato show all settings)
Executing Multiple Commands
Multiple commands can be executed on a single line by separating them with a semicolon ;. This allows to run multiple commands consecutively, one after the other. The white space between two commands is just for readability purpose.
$ uname -o; echo $SHELL; who; whoami;
GNU/Linux
/bin/bash
sujith seat0 2024-12-24 08:55 (login screen)
sujith tty2 2024-12-24 08:55 (tty2)
sujithuname -oshows the operating system type.echo $SHELLdisplays the shell being used.wholists logged-in users.whoamidisplays the current username.
Grouping together commands within parentheses:
(wc note ; ls -l note) > newlist
(wc note;ls -l note)>newlistTab Completion
When typing a directory or command name, pressing Tab will auto-complete the name if there is only one option. Double pressing Tab will show all possible options.
Typing Doc + Tab may automatically complete to Documents.
~/Documents/Odin-Project/foundations/java-script/calculator/
Doc tab O tab f tab j tab cal tab. refers to the current directory.
git add .stages all files in the current directory for Git.- In VS Code, navigate to your project directory and use
code .to open it. - Simply typing
codein the terminal launches VS Code.
Command Substitution
Shell enables connecting commands in yet another way, shell enables one or more command argument to be obtained from the standard output of another command. Which is called as Command substitution.
echo "The date today is `date` "
echo The date today is `date`
echo The date today is $(date)
# The date today is Sat Jan 4 11:33:27 AM IST 2025The last part represents the output of the date command.
The date is put inside backquotes which are not single quotes.
Using single quotes will not execute the command but treats it literally and prints whatever was inside.
In Bash it is recommended to use of the form $(command) rather than the backquotes.
