Skip to main content
added 496 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

You said in comments you want to store some commands for future use. That's the job of functions. E.g. that printf wouldcommand could be made into a function like this:

pprintln() {
    printf "%s\n" "$1"
}

which you run as pprintln "hello there", pprintln "$a" or whatever. "$1" is the first argument to the function, but of course you could read stdin instead, or use multiple arguments ("$2", "$3", ...; or all of them as a list "$@" (alike "${array[@]}")).

Similarly for the longer set of operations:

#!/bin/bash
say_hi() {
    echo "hello, $1"
}
louder() {
    echo "$1!"
}
funcs=(say_hi louder)
tmp=fuumindnames=(Huey Dewey Louie)
for funname in "${names[@]}"; do
    tmp=$name
    for func in "${funcs[@]}"; do
        tmp=$($fun$func "$tmp")
    done
    echo "result: $tmp"
done

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

You said in comments you want to store some commands for future use. That's the job of functions. E.g. that printf would be:

p() {
    printf "%s\n" "$1"
}

which you run as p "hello there", p "$a" or whatever. "$1" is the first argument to the function, but of course you could read stdin instead, or use multiple arguments ("$2", "$3", ...; or all of them as a list "$@" (alike "${array[@]}")).

Similarly for the longer set of operations:

#!/bin/bash
say_hi() {
    echo "hello, $1"
}
louder() {
    echo "$1!"
}
funcs=(say_hi louder)
tmp=fuumind
for fun in "${funcs[@]}"; do
    tmp=$($fun "$tmp")
done
echo "result: $tmp"

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

You said in comments you want to store some commands for future use. That's the job of functions. E.g. that printf command could be made into a function like this:

println() {
    printf "%s\n" "$1"
}

which you run as println "hello there", println "$a" or whatever. "$1" is the first argument to the function, but of course you could read stdin instead, or use multiple arguments ("$2", "$3", ...; or all of them as a list "$@" (alike "${array[@]}")).

Similarly for the longer set of operations:

#!/bin/bash
say_hi() {
    echo "hello, $1"
}
louder() {
    echo "$1!"
}
funcs=(say_hi louder)
names=(Huey Dewey Louie)
for name in "${names[@]}"; do
    tmp=$name
    for func in "${funcs[@]}"; do
        tmp=$($func "$tmp")
    done
    echo "result: $tmp"
done
added 496 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

Or, if you're lookingYou said in comments you want to store some commandcommands for furtherfuture use, put it in a function. That's the job of functions. E.g. that printf would be:

p() {
    printf "%s\n" "$1"
}

and then justwhich you run as p "hello there", p "$a" or whatever. "$1" is the first argument to the function, but of course you could read stdin instead, or use multiple arguments ("$2", "$3", ...; or all of them as a list "$@" (alike "${array[@]}")).

Similarly for the longer set of operations:

#!/bin/bash
say_hi() {
    echo "hello, $1"
}
louder() {
    echo "$1!"
}
funcs=(say_hi louder)
tmp=fuumind
for fun in "${funcs[@]}"; do
    tmp=$($fun "$tmp")
done
echo "result: $tmp"

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

Or, if you're looking to store some command for further use, put it in a function:

p() {
    printf "%s\n" "$1"
}

and then just run p "hello there", p "$a" or whatever.

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

You said in comments you want to store some commands for future use. That's the job of functions. E.g. that printf would be:

p() {
    printf "%s\n" "$1"
}

which you run as p "hello there", p "$a" or whatever. "$1" is the first argument to the function, but of course you could read stdin instead, or use multiple arguments ("$2", "$3", ...; or all of them as a list "$@" (alike "${array[@]}")).

Similarly for the longer set of operations:

#!/bin/bash
say_hi() {
    echo "hello, $1"
}
louder() {
    echo "$1!"
}
funcs=(say_hi louder)
tmp=fuumind
for fun in "${funcs[@]}"; do
    tmp=$($fun "$tmp")
done
echo "result: $tmp"
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

If b contains the literal string printf "%s\n" "$a", i.e. you didn't expand $a into it before hand, then yes, eval "$b" should be fine. Not sure why you'd need eval there, though, since you just have a static command. Just run printf "%s\n" "$a" directly.

Or, if you're looking to store some command for further use, put it in a function:

p() {
    printf "%s\n" "$1"
}

and then just run p "hello there", p "$a" or whatever.