Hannes
Hannes

Reputation: 255

Bash: Echoing an argument after a slash inserts a blank character

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

Upvotes: 1

Views: 237

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360065

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

Upvotes: 1

Lukáš Lalinský
Lukáš Lalinský

Reputation: 41306

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.

Upvotes: 5

Related Questions