Passing bash argument as string -
i have little , stupid problem..
i'm trying make alias tar , gzip uses file name (given argument), not converting expected filename in output.
my alias is:
alias targz='tar -cvzf $1.tar.gz $1'
it works, argument stored in $1 not working when setting filename, zips in file called ".tar.gz".
i tried echoing '$1.tar.gz' , output '.tar.gz', so, think should stupid in format.
any welcome,
aliases don't have positional parameters. they're macros (an alias gets replaced text of alias when executed).
you use function:
targz() { tar -cvzf "$1".tar.gz "$1" }
or script
#!/bin/bash tar -cvzf "$1".tar.gz "$1"
personally, i've been using following script achieve similar goal (comments added convenience):
#!/bin/bash #support multiple args arg in "$@" #strip ending slash if present (tab-completion adds them) arg=${arg%/} #compress pigz (faster on multicore systems) tar c "$arg" | pigz - > "$arg".tgz done
in case want complete version, remove argument directory if tarring , compression succeed (similar gzip individual files)
#!/bin/bash set -o pipefail arg in "$@" arg=${arg%/} tar c "$arg" | pigz - > "$arg".tgz && rm -rf "$arg" done
update: credits @mklement0 more succinct , more efficient stripping of ending slashes.