Suppose we have /path/to/my/file.gz.gg
and I would like to get only /path/to/my/file.gz
I tried to use the alias:
alias myalias="echo ${1%.gz}"
and run
myalias /path/to/my/file.gz.gg
but it doesn't work. Does anyone have any suggestions?
Suppose we have /path/to/my/file.gz.gg
and I would like to get only /path/to/my/file.gz
I tried to use the alias:
alias myalias="echo ${1%.gz}"
and run
myalias /path/to/my/file.gz.gg
but it doesn't work. Does anyone have any suggestions?
you can strip the .gg
echo "${1%.gg}"
AKAIK alias doesn't take arguments like functions or scripts, so you cannot use $1
argument, there's your problem.
Also, if it could, the dollar sign isn't escaped, so it's replaced on the fly when creating your alias command.
Here are two equivalent solutions:
$1
variable:You end up with this oneliner that does the job:
`alias myalias='fn(){ echo "${1%.gg}"; unset -f fn; }; fn'`
This would create a temporary function fn, that can take an argument, execute the function then unset it so you don't get ghost functions on your current shell.
An equivalent solution might be a simple script, eg /usr/local/bin/myalias
containing:
#!/usr/bin/env bash
echo "${1%.gg}"
So your command myalias /path/to/my/file.gz.gg
will work.
Don't forget to chmod +x
the myalias script.dd
First, this pattern %.gz
won't work, you need this %.*
or this //.gg/
and this have to be in a function
myfunction () { echo ${1%.*}; }
$ myfunction /path/to/my/file.gz.gg
/path/to/my/file.gz
And this function can be an alias)
alias myalias=myfunction
$ myalias /path/to/my/file.gz.gg
/path/to/my/file.gz