I have script that prints out few lines of text. This text is used as configuration by other process.When I normally run program I redirect standard output to file which is fine:./generateConfig.sh > normal.cfgBut the problem is that normal.cfg is really constant and I would like to give user a choice to print to file or on the screen by providing additional parameter (like -o, without parameter arguments).if the script content is:#!/bin/bashecho 8.8.8.8How would I parametrize output so that depending on args it will print out to file or stdout?This does not seem to work:#!/bin/bashoutput=/dev/stdoutif [ $# -gt 0 ]; then  output=normal.cfgfiecho 8.8.8.8 > $output
Script output depending on parameter
bash;io redirection
Several approaches:A function #! /bin/sh - [ $# -gt 0 ] && redir=true || redir=false redir() if $redir; then $@ > normal.cfg; else $@; fi redir echo 8.8.8.8An alias: #! /bin/sh - [ -z $BASH_VERSION ] || shopt -s expand_aliases [ $# -gt 0 ] && alias redir='>normal.cfg' || alias redir= redir echo 8.8.8.8(won't work with posh which disabled aliases, or if sourced (with the . command) in AT&T implementations of ksh).A dedicated file descriptor: #! /bin/sh - [ $# -gt 0 ] && exec 3> normal.cfg || exec 3>&1 echo 8.8.8.8 >&3(beware that apart from ksh93, not many shells set the close-on-exec flag on that fd 3, so you may want to close it for commands you execute by hand)