0

I have a Big problem and I don't know how can I solve this.

#!/bin/bash
function prueba(){
        valor=$1
        echo "Hola ${valor}"
}
prueba $valor

so, when i write in the console:

./beta1.sh prueba 123

print:

Hola

I expected to see:

Hola 123
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Replace `$valor` with `$2`? – Cyrus Jun 06 '21 at 20:17
  • Within the prueba function $1 is the first parameter passed to the function. Your script is calling prueba with $valor which is undefined. – roby Jun 06 '21 at 20:21
  • 1
    `$1` is only the _first_ argument. You're passing two arguments, not one -- and in a function, `$1` refers to the first argument _to the function_, not the first argument _to the script_. Because _until after the function has been run_ `valor` is empty, the function is passed no arguments at all as currently written. – Charles Duffy Jun 06 '21 at 20:32
  • Consider `prueba "$*"` – Charles Duffy Jun 06 '21 at 20:32
  • BTW, `function` is bad form; see https://wiki.bash-hackers.org/scripting/obsolete (there are entries about it in multiple tables; read them both). Better to just write `prueba() { ...` with no leading `function`. – Charles Duffy Jun 06 '21 at 20:37

0 Answers0