1

I'm trying to construct a path using a command-line argument in bash. I added the following line to my .bashrc:

alias hi="echo '/path/to/$1'"

However, this yields:

~$ hi foo
/path/to/ foo

Any idea where the blank after the slash is coming from?

Thanks

Hannes

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
Hannes
  • 245
  • 1
  • 4
  • 10

2 Answers2

5

In short, aliases can't take arguments. You can make a function instead:

 $ function hi() { echo "/path/to/$1"; }
 $ hi foo
 /path/to/foo

Read here for other options.

Community
  • 1
  • 1
Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
1

As Lukáš Lalinský stated, aliases don't take arguments, so $1 is null. However, even if you were to do this:

alias hi="echo '/path/to/'"

you'd get a space. The reason for this is so that if you had an alias like this:

alias myls=ls

and did:

myls filename

it wouldn't try to run:

lsfilename
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439