Skip to main content
On this page
On this page

Shells

Last updated: 5 Aug 2020 ·
Posted in wiki#notes

bash

<Ctrl-r> # reverse-i-search
set -o vi # Enable vi mode in Bash

Bash scripting tip:

> var="apple orange"
> echo $var
apple orange

> echo $var | awk '{print $1}'
apple
> time ( echo $var | awk '{print $1}' )
apple
( echo $var | awk '{print $1}'; )  0.01s user 0.00s system 107% cpu 0.008 total

> echo $var | awk '{print $2}'
orange
> time ( echo $var | awk '{print $2}' )
orange
( echo $var | awk '{print $2}'; )  0.00s user 0.01s system 107% cpu 0.008 total

awk takes ~0.008 seconds whereas these builtin alternatives take just ~0.002 seconds

> echo "${var%% *}"
apple
> time ( echo "${var%% *}" )
apple
( echo "${var%% *}"; )  0.00s user 0.00s system 71% cpu 0.002 total

> echo "${var##* }"
orange
> time ( echo "${var##* }" )
orange
( echo "${var##* }"; )  0.00s user 0.00s system 72% cpu 0.002 total

zsh

bindkey -v # vi mode in zsh shell
bindkey '^R' history-incremental-search-backward # Backward reverse search in zsh
take <dir_name> # Create a new directory `dir_name` and change to it

# Batch rename files with `zmv`
autoload zmv
zmv -n '(*).log' '$1.txt' # Preview the result of a move without making any actual changes

<Ctrl-q>
# "Parks" the command you're currently typing and takes you back to the prompt,
# letting you start over and type another command. Once you run that other command,
# the original command is un-parked and refills the command line.