Shell-based Linux commands are commands that are executed within a shell environment in Linux. The shell is a command-line interface that provides users with an interface to interact with the underlying operating system. The shell interprets commands entered by the user and performs actions based on the commands.
These below commands are used to perform various system administration tasks, such as listing files, changing directories, displaying system information, and more.
echo
echo is a shell command that outputs the string passed as an argument to the standard output (i.e. the terminal). Some common options for echo are:
- -n: suppresses the newline character at the end of the output
- -e: enables the interpretation of backslash escapes in the string passed as an argument, such as \n for a newline or \t for a tab character.
Example usage:
Print "Hello World!"
Print "Hello World!" along with the shell:
Enable interpolation of a backslash:
env
env is a shell command used to display environment variables and their values.
Syntax:
Options:
- -i: run the utility with only specified environment variables, discarding all previously set variables.
- -u: unset specified variables.
- -P: use full pathnames in the output.
- -0: end each output line with 0 bytes rather than a new line.
Examples:
Display all environment variables and their values:
Display the value of the SHELL environment variable.
unset the SHELL environment variable:
Note: The environment variables can be set, modified, or unset using the export, unset, and set commands respectively.
set
"set" is a shell built-in command used in various Unix-like operating systems including Linux to set or modify environment variables. An environment variable is a value stored in the system that can be accessed by various applications and scripts.
The syntax for the "set" command is:
Examples:
Set an environment variable: set VARNAME=value
Display all environment variables: set
Remove an environment variable: set VARNAME=
Change the value of an environment variable: set VARNAME=newvalue
export
export is a built-in shell command in Unix-like operating systems that sets or updates the values of environment variables. Environment variables are values that affect the behavior of the shell and programs started from the shell. export allows you to make a shell variable available to child processes of the shell.
Usage:
Options:
- -p: Displays a list of all exported variables.
- -f: Forces the removal of read-only variables.
export VAR=value: sets the value of the VAR environment variable to value.
export -p: displays a list of all exported variables and their values.
It is common to use the export command in shell scripts to set environment variables required by the script.
alias
The "alias" command in Linux is used to set a shorthand or alias name for a specified command. The format is:
For example, to set an alias for the "ls" command, you can run:
alias ll='ls -l'
This creates an alias named "ll" for the "ls -l" command, so you can run "ll" instead of "ls -l".
Aliases are temporary and only last for the current session. To make them permanent, add the alias command to your shell startup file, such as ".bashrc" or ".bash_profile".
unalias
"unalias" is a built-in shell command in most Unix-like operating systems (including Linux). It's used to remove an alias from the current shell environment. An alias is a nickname for a shell command or a set of commands, and it's created with the "alias" command.
Syntax:
Options:
- -a: Remove all aliases.
- name: The name of the alias to be removed.
Examples:
To remove an alias "ll" for "ls -l":
source
"source" is a shell command in Linux/Unix-like operating systems that reads and executes commands from a specified file in the current shell environment. It acts like running the commands in the current shell, instead of starting a new shell.
Syntax:
Example:
source command is commonly used to run shell scripts and configure shell environment variables or settings in the current shell session, instead of creating a new sub-shell or process.
.
"." is a shorthand for the source command in a Unix-style shell. The source command is used to run a shell script in the current shell environment, rather than creating a new sub-shell to run the script. The script is executed in the current shell environment, and any changes to environment variables or other configuration settings made within the script will persist after the script has been completed. The "." command is equivalent to a source and can be used interchangeably.