Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active September 29, 2023 10:43
Show Gist options
  • Save weshouman/2583a6060487d1b9c9164a59330595f6 to your computer and use it in GitHub Desktop.
Save weshouman/2583a6060487d1b9c9164a59330595f6 to your computer and use it in GitHub Desktop.
bash tips and tricks

Execute but Keep the Current Working Directory

following code changes cwd to ./step1

cd step_1
./build.sh
./test.sh

adding braces gets the code executed w/o affecting the cwd

(cd step_1 && ./build.sh && ./test.sh)

Special stuff

The bash Variable Index, don't expect info, just index.

Informal and meaningful illustration is following

  • $1, $2, $3, ... are the positional parameters.
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
  • "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
  • $# is the number of positional parameters.
  • $- current options set for the shell.
  • $$ pid of the current shell (not subshell).
  • $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
  • $IFS is the (input) field separator.
  • $? is the most recent foreground pipeline exit status.
  • $! is the PID of the most recent background command.
  • $0 is the name of the shell or shell script.

Formal and more lengthy illustration is following

Special parameters

The official list.

Special Variables

The official list.

Coloring terminals

Follow this https://chrisyeh96.github.io/2020/03/28/terminal-colors.html

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment