1

I have practiced for flow control, referencing a sample script like;

if [ $(id -u) = "0" ]; then
        dir_list="/home/*"
    else
        dir_list=$HOME
    fi
    for home_dir in $dir_list; do

The above is partly quoted lines from "Flow Control - Part 3" http://linuxcommand.org/lc3_wss0130.php

Then,to check how the wildcard in a variable behaves in for flow control, I tried the lines below;

#!/bin/bash

binlist="~/bin/*"
   
for i in $binlist; do
echo $i
done

I wanted the wildcard to expand and all files in ~/bin/ to be displayed as outputs, but it did not happen. The output is just ~/bin/*.

If I do not use the variable, and directly assign ~/bin/* into the list of for, what I expect happens, all files in ~/bin/ are displayed.

QUESTION=====

How can I enable for a wildcard in variable to expand?

Or am I misunderstanding what the sample code of the reference site intends?

=============

Thank you for your reading my question!

  • 1
    binlist=$(ls ~/bin/*) – Stephen Boston Aug 23 at 7:06
  • 1
    I would use an array instead binlist=(~/bin/*), easier to handle filenames with for example spaces and such. – bac0n Aug 23 at 7:19
  • @StephenBoston Hello! I have tried your advice. If -x option is set, like ` #!/bin/bash -x`, that works. But if it is not set, does not. Now I am confusing why no -x set script does not work. – Captain Cookie Aug 24 at 9:16
  • @bac0n Thank you for your commenting. As you are recommending to use the array, is it pointing to binlist=(~/bin/*)? Is what you mean that I could use parentheses instead of quotations? – Captain Cookie Aug 24 at 9:22
  • 1
    That looks like it should be another question. Happy to see that you have a working script now. – Stephen Boston Aug 24 at 12:57
1

You can use the bash variable for your home directory instead of ~.

The bash variable for your home directory is $HOME so your script should look like this:

#!/bin/bash

binlist="$HOME/bin/*"
   
for i in $binlist; do
echo $i
done

Alternatively, you could use /home/$USER instead of $HOME like this:

#!/bin/bash

binlist="/home/$USER/bin/*"
   
for i in $binlist; do
echo $i
done

You can view each one of these variables by running the following commands:

echo $HOME
echo $USER

These are listed under "Shell Variables" on the bash manpage.


Also, as mentioned by @John1024, the tilde will not expand to $HOME if it is placed within quotes so your third option would be to use the following:

#!/bin/bash

binlist=~/"bin/*"
   
for i in $binlist; do
echo $i
done

and as mentioned by @bac0n the quotation marks are not necessary here so you can also use the following:

#!/bin/bash

binlist=~/bin/*
   
for i in $binlist; do
echo $i
done
| improve this answer | |
  • I have gotten what is my mistake thanks to your advice. Especially, the third option is new for me. I have always put characters between double quotations. Thank you! – Captain Cookie Aug 24 at 8:51
  • 2
    Globing will not occur until the variable is used as an unquoted command argument (example: echo *) . ~/bin/*, ~/bin/\*, ~/"bin/*" will all mean the same thing. In general, it is unnecessary to quote a string that does not contain characters that will interfere with the setting of the variable. – bac0n Aug 24 at 12:19
  • 1
    @CaptainCookie I believe A blank space would be an example of a character that would interfere if not within quotes. For example, you would need to use quotes for the following: ~/"my directory/*" which would not be the same as: ~/my directory/* because of the space between my and directory. However it's not really necessary for ~/"bin/*" – mchid Aug 26 at 0:29
  • 1
    @CaptainCookie "does not contain characters that will interfere" :) – mchid Aug 26 at 0:32
  • 1
    Hello, @mchid ! I had always used quotations, but now, I've gotten it is no needed in some case. Thank you for your additional support. – Captain Cookie Aug 26 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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