[SOLVED] Bash command substitution but with bash variables
by gerard4143 from LinuxQuestions.org on (#6MF0E)
I have a simple bash script that prompts the user for a command and a filename and it works with this input:
Enter command: ls
Enter filename: datafile
but if I enter:
Enter command: ls -l
Enter filename: datafile
Then the program fails with:
line 10: ls -l: command not found
Code:#! /usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
read -r -p 'Enter command: ' cmd
read -r -p 'Enter filename: ' fName
#IFS=$' \n\t'
ans=$(${cmd} "${fName}")
echo "${ans}"If I remove the comment on the second IFS=$' \n\t' then the command substitution will work with both ls and ls -l.
What's going on here?
Enter command: ls
Enter filename: datafile
but if I enter:
Enter command: ls -l
Enter filename: datafile
Then the program fails with:
line 10: ls -l: command not found
Code:#! /usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
read -r -p 'Enter command: ' cmd
read -r -p 'Enter filename: ' fName
#IFS=$' \n\t'
ans=$(${cmd} "${fName}")
echo "${ans}"If I remove the comment on the second IFS=$' \n\t' then the command substitution will work with both ls and ls -l.
What's going on here?