And the actual tips:
אני ממש אוהב Bash.
עם השנים אספתי כל מני טריקים שימושיים.
להלן כמה מהם:
# This will echo and multiply the variable, according to the number of commas in the curly braces:
[root@lab1 ~]# num=1
[root@lab1 ~]# echo $num{,,}
1 1
# Simply delete the contents of a file
[root@lab1 ~]# > somefile.txt
# Make text substitutions similarly to SED. This will replace only the first occurrence:
[root@lab1 ~]# var=Eyal_Cohen
[root@lab1 ~]# echo ${var/Cohen/Levi}
Eyal_Levi
# The extra '/' will replace all:
[root@lab1 ~]# echo ${var//Cohen/Levi}
# Is greater than, in ASCII alphabetical order
[root@lab1 ~]# if [[ "$a" > "$b" ]] ; then
.....
# Remove from shortest rear (end) pattern
[root@lab1 ~]# var=Eyal_Cohen
[root@lab1 ~]# echo ${var%_Cohen}
Eyal
# Remove from longest front pattern
[root@lab1 ~]# var=Eyal_Cohen
[root@lab1 ~]# echo ${var##Eyal_}
Cohen
# Find the length of a string:
[root@lab1 ~]# var=foobar
[root@lab1 ~]# echo ${#var}
6
# Set default values to variables. Logic: if the variable's value is not already set, set it as default:
FIRST_POSITIONAL_PARAM=${1:-”
www.google.com”} # If the '$1' positional parameter is not given, set it to '
www.google.com'
# A way to extract a specified number of characters from a variable:
[root@lab1 ~]# var=1948
[root@lab1 ~]# echo ${var:0:2}
19
זה הכל לבינתיים. אם גם אתם אובססיביים ואספתם דברים מעניינים על הדרך - שתפו.