HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page
  • Expanding Arithmetic Expressions
  • Expanding Variables

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Getting to Know Linux
  5. Using the Shell
  6. Shell Commands
  7. Connecting and Expanding Commands

Expanding Commands

With command substitution, you essentially let the shell handle a command's output rather than the command itself. This nifty trick allows one command's output to be passed as an argument to another command. You've got two ways to do this:

  1. $(command)

  2. `command`

Just a heads up, those are backticks on the latter, not your usual single quotes.

In this approach, the command can have all the bells and whistles: options, metacharacters, and arguments. Here's an example of command substitution in action:

vi $(find /home | grep xyzzy)

In this example, the command substitution is done before the vi command is run. It runs as follows:

  1. First, the find command starts at the /home directory and prints out all of the files and directories below that point in the filesystem

  2. The output is piped to the grep command, which filters out all files except for those that include the string xyzzy in the filename

  3. Finally, the vi command opens all filenames for editing (one at a time) that include xyzzy.

If you run the above steps and are not familiar with vi, you can type :q! to exit the file.

This particular example is useful if you want to edit a file for which you know the name, but not the location. As long as the string is uncommon, you can find and open every instance of a filename existing beneath a point you choose in the filesystem.

In other words, don’t use grep from the root filesystem or you’ll match and try to edit several thousand files.

Expanding Arithmetic Expressions

Occasionally, you might fancy passing some maths results over to a command. There are a couple of ways you can expand a mathematical expression and hand it over to the shell:

$[expression]

or

 $(expression)

Expanding Variables

Variables that store information within the shell can be expanded using the dollar sign $ metacharacter.

When you expand an environment variable on a command line, what you'll see printed is the variable's value, not its name, like this:

ls -l $HOME

PreviousSequential CommandsNextShell Variables

Last updated 1 year ago

Was this helpful?

Arithmetic expression is interpreted first, and passed to echo for printing to the terminal
The result of the commands, once complete, is passed to echo for printing to ther
Using $HOME as an argument to ls -l causes a long listing of the home directory to be printed