Shell Linux Command

Shell Linux Command

February 02, 2023 6 min read 1 views 0 discussions
Table of Contents

    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. 
    username@TechnoScience:~$ echo Hello World! Hello World!
    Hello World! Hello World!
    username@TechnoScience:~$
    username@TechnoScience:~$ echo -n Hello World! Hello WorldWorld!
    Hello World! Hello WorldWorld!username@TechnoScience:~$
    username@TechnoScience:~$ echo -e "Hello\tWorld\n" Hello World
    HelloWorld
     Hello World
    username@TechnoScience:~$
    env [OPTION]... [-] [NAME=VALUE]...
    username@TechnoScience:~$ env
    LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
    XTERM_256_COLORS=1
    LESSCLOSE=/usr/bin/lesspipe %s %s
    XDG_MENU_PREFIX=moksha-
    LANG=en_IN
    GDM_LANG=en_US
    DISPLAY=:0.0
    QT_STYLE_OVERRIDE=gtk
    E_DATA_DIR=/usr/share/enlightenment
    OLDPWD=/home
    <SNIP>
    username@TechnoScience:~$ env | grep "SHELL"
    SHELL=/bin/bash
    username@TechnoScience:~$
    username@TechnoScience:~$ env -u SHELL
    set [option(s)] [variable(s)]
    username@TechnoScience:~$ set VARNAME=value
    username@TechnoScience:~$ set
    username@TechnoScience:~$ set VARNAME=
    username@TechnoScience:~$ set VARNAME=newvalue
    export [variable=value]
    mrdev@kali:~$ export VAR=value
    mrdev@kali:~$ export -p
    alias <alias_name>='<command>'
    username@TechnoScience:~$ alias ll='ls -l'
    username@TechnoScience:~$
    username@TechnoScience:~$ ll
    total 40
    -rw-rw-r-- 1 username username  298 Feb  2 23:30 archive.zip
    drwxr-xr-x 2 username username 4096 Feb  2 23:38 Desktop
    drwxr-xr-x 2 username username 4096 Feb  1 18:22 Documents
    -rw-rw-r-- 1 username username  192 Feb  2 20:40 dogs
    drwxr-xr-x 2 username username 4096 Feb  2 15:43 Downloads
    -rw-rw-r-- 1 username username    0 Feb  2 22:54 file1
    -rw-rw-r-- 1 username username    0 Feb  2 22:54 file2
    -rw-rw-r-- 1 username username    0 Feb  2 22:54 file3
    drwxr-xr-x 2 username username 4096 Feb  1 18:22 Music
    drwxr-xr-x 2 username username 4096 Feb  1 18:22 Pictures
    drwxr-xr-x 2 username username 4096 Feb  1 18:22 Public
    drwxr-xr-x 2 username username 4096 Feb  1 18:22 Templates
    drwxr-xr-x 2 username username 4096 Feb  1 18:22 Videos
    username@TechnoScience:~$
    unalias [-a] name
    username@TechnoScience:~$ unalias ll
    username@TechnoScience:~$ ll
    ll: command not found
    username@TechnoScience:~$
    username@TechnoScience:~$ unalias -a
    source filename
    . filename
    username@TechnoScience:~$ cat script.sh
    #!/bin/bash

    echo "Hello World!"
    username@TechnoScience:~$ source script.sh
    Hello World!
    username@TechnoScience:~$

    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.
    Examples:

    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":

    To remove all aliases:

    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.

    Community Q&A