Leveling up in Bash #1: Commands & Arguments

Xavier Briand
Jan 29 · 2 min read
The Bash Guide — A quality-driven guide through the shell’s many features.

Using:

This guide gets a good score on https://wiki.bash-hackers.org/scripting/tutoriallist.

Programs location

When running a program, if the command name doesn’t contain a /, bash checks if it’s a built-in or a function then searches that command in a serie of folders —listed in$PATH — until it finds a match. type [-aftpP] name displays this decision making.

A command name containing a / is considered direct pathname to the command.

Arguments

Arguments are blank spaced (eg. space, tab) delimited: word splitting.

Arguments that (might) contain blank spaces must either be quoted (using ' or ") or their blank spaces escaped (using \).
Single quoted string transform everything in the string in literals, double quoted string allow expansions. So $ rm foo bar$ rm 'foo bar'. With var='foo bar', $ rm $var$ rm "$var".

Redirections

Redirections allow to manage processes’ file descriptors. Processes have 3 default file descriptors:

  • FD 0: standard input (stdin)
  • FD 1: standard output (stdout)
  • FD 2: standard error (stderr)

Via the terminal app, the bash process has its stdin connected to the keyboard stream, and stdout and stderr connected to the terminal display stream.

File redirections

[x]>file redirects FD x (default to FD 1) to file.
eg. $ ls not-here 2> errors.log.

[x]>>file redirects FD x and append to file.

Shortcut: &>file = >file 2>&1, &>>file = >>file 2>&1

[x]<file redirected file to FD x (default to FD 0).
eg. $ cat < errors.log

File descriptor copy

[x]>&y copies FD y stream connection to FD x.
eg. $ ls not-here > all.log 2>&1: Redirect stdout to all.log, then copy stdout stream connection to stderr.

Also works like x<&[y].

Here documents/strings

Make stdin read from a string.

<<<[-]delimiter
string
delimiter

eg.

$ cat <<.
Hello world!
My name is $name.
.

<<<string.
eg. $ cat <<<"Hello World! My name is $name.".

My journey into Cybersecurity

My experience trailblazing the practice of Cybersecurity

Xavier Briand

Written by

50% solution finder at @ExperiencePoint / 50% endurance cyclist. Will train for food and burn it for adventures.

My journey into Cybersecurity

My experience trailblazing the practice of Cybersecurity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade