Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Say I declare variable var as var="ls | grep a". Then I would expect the command $var to work exactly as ls | grep a, but instead the | char is read as escaped \| so that it rather prints an error saying that no file named \|, grep or a is found.

My question is: how can I make Bash interpret | in var's value as not escaped?

I have to use it in a case statement such as

var1="str1|str2"
var2="str3|str4"
case $option in
  ${var1} ) echo "It works! 1" ;;
  ${var2} ) echo "It works! 2" ;;
  *  ) echo "It does not work"
esac

(edit2): and make it return It works! 1 if $option is either str1 or str2, and It works! 2 if $option is either str3 or str4. That is because I am writing a function in which case patterns may change according to the user inputs.

Though I don't think it is relevant to know, I use lxterminal on Lubuntu 12.04 and $BASH_VERSION is 4.2.25(1)-release.

Any help is appreciated, thank you!

share|improve this question
    
It would be very helpful that you explain what your trying to achieve. It seems you're taking the wrong path when putting compound commands in a variable... there's probably a much more robust, safer, cleaner and easier way to achieve your goal. Please see this link about the XY problem. Besides, parsing the output of ls is a very bad thing to do! –  gniourf_gniourf Oct 5 '13 at 17:50
    
I just want to have a case statement whose patterns depend on the values of some variables. In order to achieve this I thought it best to use variables as patterns, but the problem is that the vertical line is read as \|. Any solution is fine to me, I just wrote what I tried. –  AndreasT Oct 5 '13 at 18:10
    
I really have no idea what you want to do and why. If you try case $var in *b*) echo "It works!";; it will work. –  gniourf_gniourf Oct 5 '13 at 18:12
    
E.g. if var="a|b" I would want case $option in $var ) ... to produce the same result as case $option in a|b ) ..., if var="h|H|l|L I would want case $option in $var ) ... to produce the same result as case $option in h|H|l|L ) ... and so on. Briefly speaking, I'd like the "pattern )" to be variable. –  AndreasT Oct 5 '13 at 18:18
1  
For a menu, you can use the select bash builtin. help select for more info. This solution might be less clunky than what you're trying to do. –  gniourf_gniourf Oct 6 '13 at 8:53

1 Answer 1

up vote 1 down vote accepted

If I understand your question, this may be what you wish:

#!/bin/bash

var="a|b"
first_option=$(echo $var | cut -d"|" -f1)
second_option=$(echo $var | cut -d"|" -f2)

is_working? () {
  case $1 in 
    $first_option ) echo "It works!" ;;
    $second_option ) echo "It also works!" ;;
    *  ) echo "It does not work"
  esac
}

#1
option=a
is_working? $option

#2
option=b
is_working? $option

#2
option=c
is_working? $option
share|improve this answer
    
Thank you very much for your answer, but as @gniourf_gniourf commented I guess I wasn't very clear about what I meant. I would like the cases in the case statement to be variable as well as the option being tested. I slightly edited my question hoping it can clarify what I am looking for :) –  AndreasT Oct 5 '13 at 19:27
    
@AndreasT Nothing more then simple, check my answer now. –  Radu Rădeanu Oct 5 '13 at 19:42
    
Why reversing option and var? If var="aa|b" I would expect it "to work" if and only if $option is either aa or b but your first example would print It works! anyway. Besides, what if I had case ${option} in ${var1} )...;; ${var2} )...? –  AndreasT Oct 5 '13 at 20:03
    
I mean, your code works only for a single char ${option} value –  AndreasT Oct 5 '13 at 20:05
    
@AndreasT Ok, try it now –  Radu Rădeanu Oct 5 '13 at 20:14

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.