Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 1.9 KB

bash.adoc

File metadata and controls

81 lines (58 loc) · 1.9 KB

bash

Shell modes

A login shell is one whose first character of argument zero is a -, or one started with the --login option. An interactive shell is one started without non-option arguments and without the -c option.

Basically, when login from a tty you get an interactive login shell. When opening a terminal from within X, you get just an interactive shell.

The last type of shell, is the shell you get within a script.

Interactive login shell files

Login
  1. /etc/profile

    and then, in order of precedence, just the first readable file found is executed:

  2. ~/.bash_profile

  3. ~/.bash_login

  4. ~/.profile

Logout
  1. ~/.bash_logout

Interactive non login shell files

  1. /etc/bash.bashrc

  2. ~/.bashrc

Command line editing

Deleting
Delete to end of line

Ctrl + k

Delete to start of line

Ctrl + u

Delete to start of word

Ctrl + w

Delete to end of word

Alt + d

Moving
Go to the start

Ctrl + a

Go to the end

Ctrl + e

Go to next word (forward)

Alt + f

Go to previous word (backward)

Alt + b

Aliases

Aliases is an easy way to alias commands to keywords. For instance:

alias ll='ls -al'

But aliases cannot accept arguments. A workaround is to define as alias a function and invoke it:

alias sshls='function _sshls() { ssh $1 "ls -al";};_sshls'

and with sshls hostname the output of ls -al on the remote host is displayed.

Scripting

To exit script on error add the following:

set -e

In case of a command that returned non-true (0), script execution is stopped.


To exit your script if you try to use an uninitialised variable add the following:

set -u

Exercises

  • Print 1000 random numbers in the range 1..1000, sort them, eliminate duplicates and take the first 100

(for i in {1..1000}; do shuf -i 1-1000 -n1; done;) | sort -nu | head -n100