Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Doing this in bash, makes SUFFIX variable holding /dev/fd/[fd],

SUFFIX=<( echo "&*645\[]}#@name" | sed -r 's/[^[:alnum:]]+/_/g' )

What should I do to assign the actual result, of commands being executed in between parenthesis? So in this case SUFFIX should be assigned a value of _645_name?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

You've used the syntax for process substitution, which passes the output of a command as input to another command. But what you wanted was command substitution, which grabs the output of a command as a string. Command substitution uses $(…).

SUFFIX=$( echo "&*645\[]}#@name" | sed -r 's/[^[:alnum:]]+/_/g' )

Note that if you're using bash, you can do this with its own built-in constructs. If the text to sanitize is in the variable foo:

shopt -s extglob
SUFFIX=${foo//+([^[:alnum:]])/_}
share|improve this answer
    
Great! I was just about to ask for a builtin equivalent of doing the same thing, which of course is much more efficient and less dependent on other external commands. Thanks. –  branquito Mar 22 at 0:30
    
Please see my edit. –  branquito Mar 22 at 0:48
1  
@branquito That's a completely different question. Please don't ask unrelated questions together. (But to save you a trip, the answer is "outfile-" ENVIRON["SUFFIX"] ".txt" — there's no interpolation in awk strings, but juxtaposition is concatenation.) –  Gilles Mar 22 at 0:52

If that's your suffix the answer should be this easy:

suffix="${str##*[!_A-Za-z0-9]}"

To get the pieces with this method you can at most ever get head and tail. But you can get those:

head="${str%"${tail:="${str##*[!_A-Za-z0-9]"}"}"}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.