I have a script that installs a bunch of programs (homebrew, git, etc) and I have it log to stdout all the things that its doing along the way, but I want the ability to pass a flag, such as --no-log to turn off the logging. The logging functions are sourced from a file that has a bunch of helper scripts and I'm having trouble getting the flag to pass around to these other files.


Given (./bin/install.sh)

#!/usr/bin/env bash

# Test for known flags
for opt in $@
    case $opt in
        --no-log) export SILENT=true ;;
            e_warning "Invalid option $opt" 

source ./lib/utils

e_process "Installing Homebrew"
ruby -e "$(curl -#fkL raw.github.com/Homebrew/homebrew/go/install)"

and (./lib/utils.sh)

#!/usr/bin/env bash

logging() {
    # write your test however you want; this just tests if SILENT is non-empty
    if [ -n "$SILENT" ]; then
        "$@" &> /dev/null

# Command/Processing logging
e_process() {
    logging printf "$(tput setaf 6)┃ $(tput sgr0)$(tput setaf 7)%s...$(tput sgr0)\n" "$@"


If I run ./bin/dotfiles I expect to see the logging ┃ Installing Homebrew..., but if I run ./bin/dotfiles --no-log I expect to not see the logging, however its not working.

Output (using bash -x)

$ bash -x ./bin/install.sh
+ source ./lib/utils.sh
+ e_process 'Installing Homebrew'
++ tput setaf 6
++ tput sgr0
++ tput setaf 7
++ tput sgr0
+ logging printf '┃ %s...\n' 'Installing Homebrew'
+ '[' -n '' ']'
+ printf '┃ %s...\n' 'Installing Homebrew'
┃ Installing Homebrew...

Using the --no-log

bash -x ./bin/install.sh --no-log
+ for opt in '$@'
+ case $opt in
+ export SILENT=true
+ SILENT=true
+ source ./lib/utils.sh
+ e_process 'Installing Homebrew'
++ tput setaf 6
++ tput sgr0
++ tput setaf 7
++ tput sgr0
+ logging printf '┃ %s...\n' 'Installing Homebrew'
+ '[' -n true ']'
+ printf '┃ %s...\n' 'Installing Homebrew'
  • I don't see where, in the install script, you source the utils script. How does install.sh know how to execute e_process? – glenn jackman Mar 31 '14 at 21:33
  • Sorry @glennjackman, the script does actually source the file, so I edited to add it in the example. – iamnewton Mar 31 '14 at 21:43

You source the utils script before you set the value of the environment variable, so the global variable $silent_mode is empty.

Don't use a "temp" variable in the utils script, stick to $SILENT in those functions.

  • Similarly, in the install script, you don't need the no_log var: just export SILENT=true in the case branch. – glenn jackman Mar 31 '14 at 21:48
  • I've edited the scripts above to reflect the changes, but it still doesn't appear to suppress the logging. When I'm testing this, I'm running bash on OSX 10.9 from within the scripts directory (./bin/dotfiles --no-log) and it still shows all print statments. – iamnewton Mar 31 '14 at 21:59
  • run your script with bash -x and see what bash is doing – glenn jackman Mar 31 '14 at 22:01
  • added to the example above, using bash -x – iamnewton Mar 31 '14 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.