Bash Samples

back


Table of contents
  1. Bash Samples
    1. echo vs printf
    2. which vs whereis

echo vs printf

Both echo and printf are commands used for displaying text in the shell but they have some important differences.

While echo simply outputs the strings provided to it, printf allows for more detailed formatting of strings, numbers, and other data types.

name="Ashish"
age=28

printf "%s is %d years old.\n" "$name" "$age"

# Output: Ashish is 28 years old.

which vs whereis

The which command is used to find the location of the executable that would be run if you type the command in the shell.

The command returns looks in directories listed in the $PATH environment variable and returns the path to the executable.

which ls

# Output: /bin/ls

On the other hand, the whereis command provides more detailed information about a command which includes its binary, source files, and manual pages.

It searches in standard system directories and does not rely on the $PATH variable.

whereis ls

# Output: ls: /bin/ls /usr/share/man/man1/ls.1.gz

The output tells us where the executable binary for the ls command is located (/bin/ls) and where its associated manual/ man page is (/usr/share/man/man1/ls.1.gz).