Skip to content

Instantly share code, notes, and snippets.

@hhatfield
Created May 2, 2014 18:10
Show Gist options
  • Save hhatfield/ad80d39fef702417442e to your computer and use it in GitHub Desktop.
Save hhatfield/ad80d39fef702417442e to your computer and use it in GitHub Desktop.
Bash redirector with command name
#!/bin/bash
outfile=${*: -1:1}
remove=1
echo "$@" > $outfile; "${@:1:$(($#-remove))}" >> $outfile
tmp $ ./redirector.sh ls -la lsout
tmp $ cat lsout
ls -la lsout
total 224
drwxrwxrwt 10 root wheel 340 May 2 12:09 .
drwxr-xr-x@ 6 root wheel 204 Oct 31 2013 ..
drwx------ 3 hhatfield wheel 102 May 1 08:13 launch-KrJQJV
drwx------ 3 hhatfield wheel 102 May 1 08:13 launch-yqQeEs
drwx------ 3 hhatfield wheel 102 May 1 08:13 launchd-241.qFHX6O
drwx------ 3 _spotlight wheel 102 May 1 08:14 launchd-612.NE2bhp
drwx------ 3 root wheel 102 May 1 08:14 launchd-632.do4N0n
-rw-r--r-- 1 hhatfield wheel 13 May 2 12:09 lsout
-rw-rw-rw- 1 hhatfield wheel 103000 May 2 08:49 oobelib.log
-rwxr-xr-x 1 hhatfield wheel 98 May 2 10:49 redirector.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment