Bash: Difference between revisions
From DWIKI
Line 12: | Line 12: | ||
=Tips and tricks= | =Tips and tricks= | ||
==String manipulation== | |||
===Remove character from string=== | |||
VERSION='2.3.3' | |||
echo "${VERSION//.}" | |||
===check if var contains substring=== | |||
[[ $foo =~ .*sub.* ]] | |||
===get first character of a string=== | |||
FIRST=${STRING:0:1} | |||
==read without while loop== | ==read without while loop== | ||
Line 20: | Line 30: | ||
if [[ $foo =~ ^[0-9]+$ ]] | if [[ $foo =~ ^[0-9]+$ ]] | ||
==move cursor== | ==move cursor== |
Revision as of 14:25, 22 August 2019
- Homepage
- http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html
- Shortcuts to move faster in Bash command line
- Bash Guide for Beginners
- http://www.ling.helsinki.fi/users/reriksso/unix/shell.html
- Advanced Bash Scripting Guide
- Linux Shell Scripting Tutorial
- Aliens Bash Tutorial
- http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/
- Bash Keyboard Shortcuts
- Change colour of bash prompt
Tips and tricks
String manipulation
Remove character from string
VERSION='2.3.3' echo "${VERSION//.}"
check if var contains substring
[[ $foo =~ .*sub.* ]]
get first character of a string
FIRST=${STRING:0:1}
read without while loop
The problem is scope, try
echo foo bar | { read a b;echo $b; }
check if variable is integer
if [[ $foo =~ ^[0-9]+$ ]]
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 }