Iterate and Check if a Bash Array contains a value
Shell Tips
January 27, 2020 | COMMENTS

Version 2 of GNU Bash added support for array variables, a.k.a one-dimensional indexed arrays (or lists). Since the version 4, came the support for associative arrays (a.k.a dictionaries or hashtables). Those features simplifies heavily how you can write your scripts and support more complex logics and use cases.

In this post we will review how to declare, iterate over, and check a value of an indexed arrays and associative arrays.

1. Difference between Indexed Arrays and Associative Arrays
2. How to declare a Bash Array
3. Array Operations and How to loop over a Bash Array
4. How to Check if a Bash Array contains a value

Continue Reading
How To Upgrade your Bash Version on MacOS
Shell Tips
January 16, 2020 | COMMENTS

GNU Bash is a powerful shell, unfortunately most distribution don't provide you with the latest version which may prevent you to take advantage of the great new features that came with Bash 4 and 5. Also, running an outdated bash version probably expose you to some major vulnerabilities. For example, MacOS comes with Bash 3.x which is quite limiting and lack key features like associative arrays (dictionnaries), improved auto-completion, better Posix conformance, etc. This post cover simple steps to upgrade bash on MacOS.

Continue Reading
Advanced Math Calculation in Bash using GNU bc
Shell Tips
January 1, 2015 | COMMENTS

In this post, we will cover how to do advanced arithmetic and write your own functions with GNU bc.

If you are looking at doing basic arithmetic in a bash shell or using bc, you should take a look at my older post about Performing Math Calculation in Bash.

Continue Reading
disown a running shell process and reattach it to a new screen
Shell Tips
September 9, 2014 | COMMENTS

Stop worrying about your VPN connection dropping and your remote job to get kill. You can work on a remote server from your laptop or desktop in a safe and controlled way by detaching your screen session and reattaching later.

Continue Reading
redirecting a stdout to a file using sudo and tee
Shell Tips
September 8, 2014 | COMMENTS

When you attempt to modify a file without write permission on it, you will end-up with a permission denied error.

$ touch donottouch.txt && sudo chown root donottouch.txt
$ cat donottouch.txt
$ echo "change stuff" > donottouch.txt
-bash: donottouch.txt: Permission denied

Using sudo before echo won't help since the redirection will still apply within your shell environment. Here is few approaches to this problem.

Continue Reading