Skip to content

Linux & shell: Shell operators (how combinations work)

6 Linux & shell commands: Shell operators (how combinations work). Click a command to see what each part does.

Index
6 items
Click a command to see what each part does.

Shell operators (how combinations work)6

  • cmd1 | cmd2Send the output of one command into another

    What each part does

    1. cmd1first command; its normal output (stdout) is captured
    2. |pipe: stdout of the left command becomes the input (stdin) of the right one
    3. cmd2second command, reads that input (e.g. grep, sort, head)
  • cmd > out.txt; cmd >> out.txtSave output to a file (overwrite / append)

    What each part does

    1. >redirect stdout to a file, creating it or truncating it to empty first
    2. ;run the next command after the first, whatever its result
    3. >>append stdout to the end of the file (creates it if missing)
  • cmd > out.log 2>&1Capture errors together with normal output

    What each part does

    1. > out.logstdout (file descriptor 1) goes to out.log
    2. 2>&1make stderr (fd 2) point where stdout points. Order matters: it must come after > out.log

    Tip Short bash form: cmd &> out.log

  • make build && make deploy || echo "failed"Run the next command only if the previous one succeeded / failed

    What each part does

    1. make buildfirst command
    2. &&run the next command only if the previous exited with code 0 (success)
    3. make deployruns only after a successful build
    4. ||run the next command only if the previous exited non-zero (failure)
    5. echo "failed"fallback message
  • kill $(pgrep -f <name>)Use the output of a command as an argument

    What each part does

    1. killsend a signal (TERM by default) to process IDs
    2. $( … )command substitution: run the inner command and paste its output here
    3. pgrep -f <name>print PIDs whose full command line matches <name>
  • cat <list-file> | xargs -r -n 1 <cmd>Pass input lines as arguments to another command

    What each part does

    1. cat <list-file>print the file (one item per line)
    2. |pipe into xargs
    3. xargsbuild and run command lines from its input
    4. -rdon't run the command at all if the input is empty (GNU xargs)
    5. -n 1one input item per command invocation
    6. <cmd>the command to run with each item appended

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…