Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ls *_$var_*

17 views
Skip to first unread message

jammer

unread,
May 23, 2012, 11:21:56 AM5/23/12
to
#!/bin/bash

# manual
# mkdir _20120522_
# ls -d *_20120522_*

# script
dt='20120522'
ls "*_"$dt"_*"

jammer

unread,
May 23, 2012, 1:00:13 PM5/23/12
to
The ls works manually but not in the script.

jammer

unread,
May 23, 2012, 2:07:24 PM5/23/12
to
On Wednesday, May 23, 2012 11:21:56 AM UTC-4, jammer wrote:
ls -d *_"$dt"_* worked.

Janis Papanagnou

unread,
May 24, 2012, 3:04:11 AM5/24/12
to
You omitted option -d in the script. Without that option
ls displays the contents of the directory, which is empty.

Janis

Janis Papanagnou

unread,
May 24, 2012, 3:06:42 AM5/24/12
to
And, of course, the more significant factor; you quoted the
'*' argument, so it will be taken literally and not expanded
by the shell. Use either

ls -d *_${dt}_*

or quote the variable (as opposed to the globbing characters)

ls -d *_"${dt}"_*

> Janis

Stephane Chazelas

unread,
May 24, 2012, 8:52:01 AM5/24/12
to
2012-05-24 09:06:42 +0200, Janis Papanagnou:
[...]
> ls -d *_"${dt}"_*
[...]

ls -d -- *_"$dt"_*

or

printf '%s\n' *_"$dt"_*

--
Stephane
0 new messages