Bash cmd line tips

Find here some commandline tips.

For a deeper view about programming in bash, go to Bash programming section.

Bash history

Searching
 ctrl-r + <hint_string>

Each Ctrl-r will iterate search. [Left|Right] to edit command.

or set -o vi

for ‘VI’ command mode. In this mode, search with <ESC> + / + <str_to_search>. Use ‘n’ for next (previous) match. Or <ESC> + ['j' | 'k'] to cycle back and forward.

beginning / end of line

You can use Home and End keys here of course but alternatively, you can use Ctrl+A to go to the beginning of the line and Ctrl+E to go to the end.

No history

Several methods for not leaving history of commands…

Taking into account that each bash session has its own history buffer, written to the history file on exit, a dirty way to avoid leaving the session history is terminating abruptly the terminal with

 kill -9 $$

A very much polite way is to unset the variable HISTFILE with unset HISTFILE

This works in all versions of bash.

Also, the history -c command clears the buffer, so nothing is written (from bash 4.3 onward). It will save, though, the commands you type afterwards

In later versions of many shells, commands entered with a starting space (command) will no be put in history

History with time stamps
 export HISTTIMEFORMAT='%F %T '

Bash cmd line trick

 ctrl+u [...] ctrl+y   

type partial command, kill this command, check something you forgot, yank the command, resume typing.

Pattern Matching

Mainly from Pattern Matching from the Bash manual

* and **

Matches any string, including the null string. When the globstar shell option is enabled, and ‘*’ is used in a filename expansion context, two adjacent ‘*’s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a ‘/’, two adjacent ‘*’s will match only directories and subdirectories.

(** needs globstat to be set “true” –> shopt -s globstar)

[…]

Matches any one of the enclosed characters. A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched.

(pattern-list)

Being a pattern-list a list of one or more patterns separated by a ‘|’:

?(pattern-list) - Matches zero or one occurrence of the given patterns
*(pattern-list) - Matches zero or more occurrence of the given patterns
+(pattern-list) - Matches one or more occurrence of the given patterns
@(pattern-list) - Matches one of the given patterns

Shell expansions

Braces

   echo a{d,c,b}e               --> Oututs "ade ace abe"  (in one single line)
   mkdir ./{old,new,dist,bugs}  --> Creates old, new, dist and bugs directories
   rm -r ./{old,new,dist,bugs}  --> Deletes old, new, dist and bugs directories

Can also use wildcards

  chown root /test/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}   

Parameter

-Use a default value-

  ${parameter:-word}

If the parameter is unset or null, this one EXPANDS to WORD, otherwise it expands to the value of PARAMETER but it only happens that time: WORD is not assigned (so not set) to parameter

-Assign a default value-

  ${parameter:=word}

If the parameter is unset or null, this one ASSIGNS to WORD, otherwise it expands to the value of PARAMETER. Once assigned, WORD is assigned (so set) to parameter and useful afterwards

With the ‘:‘, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

String length

  ${#parameter}
    $ MYSTRING="Be water my friend"
    $ echo ${#MYSTRING}
    18

Positional parameters shift

  ${@:length}

Length positional parameters beginning at offset

    $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
    $ echo ${@:7}
    7 8 9 0 a b c d e f g h