User Tools

Site Tools


linux:bash

This is an old revision of the document!


BASH

WILDCARD METACHARACTERS & EXPANSION

The shell expands special characters (globs/wildcards) before passing arguments to commands. This is called pathname expansion (or globbing).

Core wildcards:

  • * - zero or more characters. Example: ls *.jpg - all .jpg files in the current directory
  • ? - exactly one character. Example: cp report?.pdf /backup/ - report1.pdf, reportA.pdf, etc.

For example, following commands prints a list of files in the current directory:

$ echo *

What's happening:

  1. Globbing (before echo runs) - The shell — not echo — expands * by looking at the current directory and replacing * with a space-separated list of matching filenames.

Disabling expansion

  • If you don’t want the shell to expand a glob in a command, enclose the glob in single quotes (' '). For example, the command echo '*' prints a star.
  • This might be useful when running commands like find, to pass patterns containing wildcards
  • The reason why find works sometimes is because shell expansion only happens if matches exist in the current directory

HERE DOCUMENTS

Here document is just another form of I/O redirection where we embed a body of text into our script, the format is:

command << token
text
token

Example:

cat << _EOF_
hello
here
_EOF_

If we change the redirection operator from « to «- the sell will ignore leading tabs.

COMMAND SUBSTITUTION

Command substitution allows us to use the output of the command as expansion (substitution).

Basically it allows the output of the command to replace the command itself.

$ CURRENT_DATE=$(date)   # CURRENT_DATE will be assigned the output of date command

ENVIRONMENT VARIABLEs

Environment variables are named values stored by the shell/OS that processes can read to configure their behavior. Think of them as a key-value store available to every running program.

Environment variables are not global system state. They are per-process (and inherited downward only).

Setting an env variable for the current session:

export STUFF=blah

To make env variable permanent, you need to store it in a file. Which file - depends on the Shell type:

Type of Shells:

Shell Type When it starts Example Reads file
Login Shell You authenticate (SSH, TTY login, su -, bash -l) SSH into a server ~/.profile
Interactive non-login You open a new terminal in an existing session New tab in your terminal emulator ~/.bashrc
  • ~/.profile — runs once per login session. Good for:
    • export PATH=…, export EDITOR=vim
  • ~/.bashrc — runs every time you open a new interactive bash shell. Good for:
    • aliases, shell functions, prompt

REDIRECTS vs PIPES

The core difference: redirects connect a stream to a file (a redirect always has a file on one side). Pipes connect one process to another process.

Pipes: process → process

STANDARD INPUT and OUTPUT

Unix programs have 1 input and 2 outputs.

When you run a command from a terminal, they all go to/from the terminal by default, e.g.:

$ cat
hello   # Stdin is connected to the terminal, you can type there.
hello   # Stdout - cat prints it right away after you pressed enter.

< redirects Stdin

cat < foo.txt
bar
linux/bash.1778851768.txt.gz · Last modified: by v1ctor