Bash scripting tricks
Bash tricks
- Check bash version in script
if ((BASH_VERSINFO[0] < 4)) then echo "Sorry, you need at least bash-4.0 to run this script." exit 1 fi
- Reading columns from a file in bash array
table_collection=( $(cut -d $'\t' -f1 table_data.dat) )
The above reads column 1 (f1 – field 1) in table_collection array
- Compress individual files in a directory
for i in *; do tar -czvf $i.tar.gz $i; done
- Forward declaration in bash or shell script
#!/bin/bash main() { foo bar baz } foo() { } bar() { } baz() { } main "$@"
- Let screen load your .bash_profile
Edit ~/.screenrc to containshell -$SHELL
this will make screen start-up using a login shell which will load your ~/.bash_profile
https://superuser.com/questions/343541/how-to-force-gnu-screen-to-load-my-bash-profile - Coming soon…