Bash command substitution but with bash variables
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 What's going on here? |
I wanted to write, it is quite simple, it is what you have requested. But probably not that obvious:
IFS is the Input Field Separator, you set it to \n\t. Therefore ls -l is a single word (does not contain either \n or \t). Actually there is no such command, "ls -l" is not a command itself. BUT if IFS is set to $' \n\t': ls -l will be split (because it contains a space, which is now a separator) and the result is: the command will be ls and -l will be an argument to it, which is the correct way to execute it. |
Code:
- ans=$(${cmd} "${fName}") |
I tried a few experiments in my bash terminal..
If I type ls -l and hit return it produces the expected results. Code:
ls -l Code:
'ls' '-l' Code:
'''ls''' '''-l''' Code:
'ls -l' |
Regarding '''ls''' '''-l'''
Try to remove the empty strings: Code:
$ echo "'''ls''' '''-l'''" | sed "s/''//g" As far as I know, there is no way to use quotes within quotes (duplication or escaping), you have to close the quote, use a standalone quote (\' or "'"), then again a staring quote: Code:
$ echo 'she sometimes said '\''Supercalifragilisticexpialidocious'\'' I remembered' Code:
$ echo "another version of this word is \"supercaliflawjalisticeexpialadoshus\" you might add" Code:
echo 'This works in \'PHP\' language'; |
you can use set -x to see what's going on.
you type a string, press enter. First this string will be evaluated, and split into an array and finally will be executed Code:
pan@host:/tmp/a$ ls -l |
Quote:
|
By quoting you force it to be one word, no word-splitting takes place.
Differerent quoting styles: Code:
'ls -l' The same applies to the command arguments: Code:
touch "two words" Code:
'''ls''' '''-l''' The word-splitting is according to $IFS (Input Field Separators). With IFS=$'\n\t' the space is removed but the \t (tab) is still there. Then Code:
ls -l Code:
ls<tab>-l What happens with the \n (newline)? On the command line (actually readline, also read) a newline causes an end-of-input-line so it won't exist for a direct word-splitting. But the following example demonstrates it: Code:
twolines="line1 echo ... you better use printf "<%s>\n" ... for clarity; the format is applied on each argument (wrapped in < > followed by a newline). |
All times are GMT -5. The time now is 06:03 AM. |