Created
July 10, 2009 11:46
-
-
Save davidreuss/144420 to your computer and use it in GitHub Desktop.
useful bashisms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Default variable if not set | |
[ -z "${var:-}" ] && var="Default..." | |
echo "$var" | |
# array declaration | |
names=(Adam David Johnny Black Abraham) | |
# array expansion | |
for item in ${names[@]}; do | |
echo "$item" | |
done | |
# array count | |
echo ${#names} | |
# delete pattern in array | |
echo ${names[@]##A*} # David Johnny Black | |
# string operations | |
str="thisisaverylongstring" | |
# substrings | |
echo ${str:4} # isaverylongstring | |
echo ${str:6:5} # avery | |
echo ${str%string} # thisisaverylong | |
# replaces first occurence | |
echo ${str/i/_i_} # th_i_sisaverylongstring | |
# replaces every occurence | |
echo ${str//i/_i_} # th__i__s__i__saverylongstr__i__ng |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First one is not Bashism. It is defined by POSIX: http://pubs.opengroup.org/onlinepubs/9699919799//utilities/V3_chap02.html