0

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?

user321627
  • 2,350
  • 4
  • 20
  • 43

3 Answers3

1

you can strip the .gg

echo "${1%.gg}"
on8tom
  • 1,766
  • 11
  • 24
  • I tried doing `alias myalias="echo "${1%.gg}""` but it still returns the extension. Can this be put into an alias? – user321627 Jun 23 '20 at 10:30
1

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. Wrap your alias in a function so you get to use $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.

  1. Another solution: Use a wrapper script

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

Orsiris de Jong
  • 2,819
  • 1
  • 26
  • 48
1

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
Ivan
  • 6,188
  • 1
  • 16
  • 23