Article 6MF0E [SOLVED] Bash command substitution but with bash variables

[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?
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments