Programming in Unix Environment: Using the Shell

Command Line Structure

In a Unix shell, commands are typically executed by pressing Enter. However, there are different ways to structure and control the execution of commands:

  • Command Terminators:
    • A command usually ends with a newline but can also be terminated with a semicolon (;).
    • Parentheses () can be used to group commands and execute them in a subshell.
    • The ampersand (&) allows a command to run in the background, letting the user continue with other tasks.
  • Redirection and Piping:
    • The pipe (|) allows passing the output of one command as input to another.
    • The tee command captures output from a pipeline and writes it both to a file and to the standard output.

Examples:

# Grouping commands using parentheses and piping output to wc (word count)
$ (date ; who) | wc

# Capturing output in a file and continuing the pipeline
$ (who ; date) | tee output.txt | wc

# Running a long-running command in the background
$ long-running-command &

# Sleeping for 5 seconds before executing date
$ sleep 5 ; date

# Running a command in the background while executing another immediately
$ (sleep 5 ; date) & who

In the last example, who executes immediately, while (sleep 5 ; date) & waits for 5 seconds before printing the current date in the background.

Metacharacters in the Shell

Metacharacters have special meanings in Unix shells. To use them literally, enclose them in single quotes (').

Example:

$ echo '**'  # Prints ** instead of interpreting * as a wildcard

Other common metacharacters include:

  • * (Wildcard for multiple characters)
  • ? (Wildcard for a single character)
  • {} (Brace expansion)
  • [] (Character class matching)
  • $ (Variable substitution)
  • > and < (Redirection operators)
  • | (Pipe operator)

Escaping Metacharacters

If you need to use a metacharacter without its special meaning, escape it using a backslash (\) or enclose it in single quotes:

$ echo \$HOME    # Prints the string "$HOME" instead of expanding it to the home directory
$ echo 'Hello > World'  # Prints "Hello > World" instead of treating '>' as a redirection operator

Additional Notes

  • The nohup command allows a process to continue running after the user logs out.
  • Job control commands like fg, bg, and jobs help manage background processes.
  • Using &> redirects both standard output and standard error to a file.

Example of nohup:

$ nohup long-running-command &  # Keeps running even if the session is closed

Unknown's avatar

Author: Abhilash

Hi, Iโ€™m Abhilash! A seasoned web developer with 15 years of experience specializing in Ruby and Ruby on Rails. Since 2010, Iโ€™ve built scalable, robust web applications and worked with frameworks like Angular, Sinatra, Laravel, Node.js, Vue and React. Passionate about clean, maintainable code and continuous learning, I share insights, tutorials, and experiences here. Letโ€™s explore the ever-evolving world of web development together!

Leave a comment