I am trying to create a simple alias that uses the argument with the full path
On the command line, I can type command "$(pwd)/my_file"
. It works. so I tried to create an alias in the following way:
alias command='command "$(pwd)/$1"'
This alias didn't work though. The CLI interprets as if $(pwd)
and my_file
were separated arguments... I tried to use the eval
command to turn my command into a single one
alias command="eval 'command' '$(pwd)/$1'"
However, it keeps waiting for an input argument instead of taking my initial argument...
If you wanna try out what I mean, substitute command
for the evince
(a popular PDF viewer) and my_file
for any PDF file. So my alias is
alias evince="evince $(pwd)/$1"
In my case, $(pwd)
is /home/tapyu/Downloads/
, and my_file
is recap.pdf
. I know that evince is treating it as a separated argument because it pops up two windows: The first one opens recap.pdf
properly. The second is an empty window with a warning "Error opening file, /home/tapyu/Downloads/ is a directory."
Thank you in advance.
PS: I know that this "problem" is pointless. My problem is not "how to give the full path to a command", my problem is "how to handle inputs argument in an alias in order to solve this kind of situation". So don't wanna alternatives to give the full path, I wanna know why my alias is not working.
$(pwd)
andmy_file
as two separate arguments when you define your alias as you show first? How do you actually try to use that alias? Also, aliases do not take arguments. They are simple text replacements. Your second alias makes no sense unless you have set$1
to something at the time of defining the alias (but it's still not what you want, I guess). I believe you may be wanting to use a shell function, but I'm still confused about your talk about "separate arguments". – Kusalananda Aug 09 '21 at 07:44command my_file
and that is exactly the same ascommand $(pwd)/my_file
. – terdon Aug 09 '21 at 09:09alias command="eval 'command' '$(pwd)/$1'"
, the shell keeps expanding it recursively forever (useset -x
to see it). You may use... eval '\command' ...
instead (the backslash prevents alias expansion), but all the points in the previous comments would still stand. – fra-san Aug 09 '21 at 09:17evince
example) it opens two windows: The first one opens thefile
properly. The second is an empty window with a warning "Error opening file,<pwd-path>
is a directory.". – Rubem Pacelli Aug 09 '21 at 15:32pwd
. I can't reproduce what you describe. The normal behavior is thatalias command='command "$(pwd)/my_file"'
would work butalias command="eval 'command' '$(pwd)/$1'"
would not since$1
wouldn't be defined. So if you are seeing something else, that will be because of the specific paths and/or file names you are using. Most likely you have whitespace somewhere in either the path or the file name. The main point is that aliases cannot take arguments though. – terdon Aug 09 '21 at 15:43command othername
would callcommand $(pwd)/my_file othername
, so the two arguments are$(pwd)/my_file
andothername
. Is that what you are seeing? I find it very frustrating that you don't say exactly what's happening. It would be easy to tell you to usemycommand () { evince "$PWD/$1"; }
if we knew what you wanted to do, but you don't really say what effect you are looking for. – Kusalananda Aug 09 '21 at 15:55\command
solved the problem of recursion. Nevertheless, the commandalias command="eval '\command' '$(pwd)/$1'"
still treats'$(pwd)/$1'
as two separated arguments. – Rubem Pacelli Aug 09 '21 at 16:46alias evince='evince...
? Or are you using different names? – terdon Aug 09 '21 at 16:58