0

I was trying to create an alias like this.

alias foo='function bar(){echo "$1:localhost"};bar'

But when I execute foo 123, it shows 123ocalhost, so :l is missing.

I also tried alias foo='function bar(){echo "$1:a"};bar'. Executing foo 123 produces /current_directory/123.

alias foo='function bar(){echo "$1:a"};bar' and executing foo 123 produces 123:b.

So different letters after the colon will have different results. Can someone explain this?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Your file (or whatever you getting `$1` from) is saved as a DOS text file instead of a UNIX one. That means it has a carriage return character at the end of each line. When printed, a carriage return sends the cursor to the left. – Charles Duffy Oct 13 '21 at 04:26
  • 3
    By the way -- why would you ever use an alias here instead of only defining a _function_ `foo`? It's only when defining git aliases that there's a reason to wrap functions that way; it's pointless for shell aliases. – Charles Duffy Oct 13 '21 at 04:29
  • 2
    Also, about the `function` keyword, see https://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Oct 13 '21 at 04:30
  • 1
    Another piece of advice: Use `set -x` in your script, or `bash -x your_script` when running it, to enable trace logging. That'll show you the commands being run, rendering the carriage returns as `\r` sequences inside `$''`-style quoting. – Charles Duffy Oct 13 '21 at 04:47

0 Answers0