Tag Archives: Bash

Use Alias in Bash Script

A few weeks ago, I had trouble accessing aliases in bash scripts. It turns out that aliases are not expanded in scripts, only when the shell i interactive. However, we can get around this by using shopt to expand_aliases at the top o our script.

#!/bin/bash
shopt -s expand_aliases
...

Using tmux

I found this great tutorial on tmux by Daniel Miessler that was designed to take you from “wtf tmux” to “omg tmux” with extreme haste. I’m reproducing parts of that blog post here because I find myself googling it at least three times a day. (Because it’s that useful.) Briefly, tmux is a utility that allows you to connect to a remote server, start a job, and then log off without quitting your job. When you log back in from another location, you can access that job again through tmux. The screen utility also does this, but not as well. For instance, if you have a really long username, then you might not be able to start screen, because edge cases.

Continue reading Using tmux

echo versus printf

Quick Note: When you’re bash scripting and you need to pipe some text into some process, you can use echo with the -n flag to suppress the echo‘s complimentary endline , or you can use printf, but using printf is more portable. Some machines implement echo differently and don’t support the -n flag. I found this advice and a longer explanation at this message board.

Local Variables and Return Values in BASH

In this post, I’ll discuss some lessons learned from BASH programming, in particular, how to pass return values from BASH’s “functions”. BASH does not provide support scoped variables by default, so variables declared in a function are available everywhere once the function has been called. BASH will let you declare local variables within a function through the local keyword. Returning values is then a matter of echoing them out.

Continue reading Local Variables and Return Values in BASH