0

I would like to make a string that includes an argument:

function praise () {
  echo "{$1} is great"
} 

However when I run praise BillyBob I get is great BillyBob. What am I doing wrong?

T Brigade
  • 1
  • 1
  • 3
    Weird. Can you check your file for carriage returns (like [in this answer](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings))? – that other guy Apr 03 '20 at 17:39
  • 3
    How is it Billybob changes case too! – JGFMK Apr 03 '20 at 17:40
  • @thatotherguy I just ran dos2unix on my .bash_profile, ran source on it again, and got the same result. I am on MacOS. JGFMK that was just a typo :-) – T Brigade Apr 03 '20 at 18:28
  • Would you mind copy-pasting the code from your post into a new file, run it, then copy-paste the result (including the command you used to run it) back into your post? See [this meta-question](https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough) for why it's better to copy-paste than to type it in, even if the result seems the same – that other guy Apr 03 '20 at 18:36
  • Do you also have an alias for `praise`? What does `type praise` print? – Gordon Davisson Apr 03 '20 at 19:41

2 Answers2

0

Try this:

  function praise {
    echo "${1} is great"
  } 
Saboteur
  • 1,331
  • 5
  • 12
0

I figured out the problem. The function was in my .bash_profile. When I source this file, bash doesn't update the praise function with whatever version is in the file, although aliases and variables like PS1 are updated. I don't know why praise isn't updated, but opening a fresh bash instance resolves the problem. The terminal tab I was testing in must have had an outdated version of this function that was causing the error (maybe a newline issue, but hard to say at this point). My code works as expected in a new tab.

T Brigade
  • 1
  • 1