Bash: Difference between revisions

From DWIKI
mNo edit summary
Line 9: Line 9:
*http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/
*http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/
*[https://ss64.com/bash/syntax-keyboard.html Bash Keyboard Shortcuts]
*[https://ss64.com/bash/syntax-keyboard.html Bash Keyboard Shortcuts]
*[https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ Change colour of bash prompt]


=Tips and tricks=
=Tips and tricks=

Revision as of 16:36, 22 July 2018

Tips and tricks

check if variable is integer

if [[ $foo =~ ^[0-9]+$ ]]

get first character of a string

FIRST=${STRING:0:1}

check if var contains substring

[[ $foo =~ .*sub.* ]]

move cursor

^b move back 1 char
^f move forward 1 char
alt-b move back 1 word
alf-f move forward 1 word

loops

loop through range of numbers

for i in {0..10};do echo $i;done


bash history timestamp

HISTTIMEFORMAT="%d/%m/%y %T "

screen and bash history

To make sure all screens write to same history (at least on CentOS and RHEL):

echo "history -a;history -r" > /etc/sysconfig/bash-prompt-screen
chmod +x /etc/sysconfig/bash-prompt-screen

In general:

export PROMPT_COMMAND='history -n'

functions

function foo() {
   echo $1 $2
 }