How to use the ls ? option in a bash variable?
Solution 1
This worked for me but depending on what you are doing it is probably not a good idea to leave that variable unquoted. You probably should also use something other than ls
: http://mywiki.wooledge.org/ParsingLs
foobar_ls.sh
#!/bin/bash
FOOBAR_LIST="foobar_??????.log"
ls -lt ${FOOBAR_LIST}
In order to rename these files instead you can do one of the following:
shopt -s nullglob
for file in /path/to/files/foobar_??????.log; do
mv "$file" "${file}.old"
done
shopt -u nullglob
or
find /path/to/files -type f -name 'foobar_??????.log' -exec mv {} {}.old \;
Solution 2
?
is not a feature of ls
, it's a feature of the shell, called filename expansion or wildcard expansion or pattern matching or globbing. You must let the shell perform the wildcard expansion, so that ls
receives the list of matching file names.
If you have a string with wildcards in a variable, and you want to expand these wildcards to matching file names, leave the variable substitution unquoted. This is the “split+glob” operator: the value is split into whitespace-delimited parts, and each part is replaced by the list of matching file names, except that parts that don't match any file name are left unmodified.
FOOBAR_LIST="foobar_??????.log"
ls -lt $FOOBAR_LIST
Related videos on Youtube
Ian Grainger
Updated on September 18, 2022Comments
-
Ian Grainger almost 2 years
I've created an account on Gitlab, set up an SSH key and can fetch from my new project URL - but I can't push :( - if I use HTTPS I get:
Writing objects: 100% (375/375), 84.82 MiB | 11.22 MiB/s, done. Total 375 (delta 182), reused 0 (delta 0) error: RPC failed; result=55, HTTP code = 0 fatal: The remote end hung up unexpectedly
If I use SSH I get a
TortoiseGitPlink Fatal Error
message:Server unexpectedly closed network connection
- and this in my Git output:Compressing objects: 100% (359/359), done. fatal: sha1 file '<stdout>' writeatal: The remote end hung up unexpectedly error: Invalid argument error: failed to push some refs to '[email protected]: [snip]'
It is quite a big repository. I've tried getting a newer version of TortoiseGitPlink but that didn't help.
-
Admin over 6 yearsWhat's in the script then?
-
jesse_b over 6 yearsYou can't quote wildcards. What your script is doing would be the equivalent of doing this on the command line:
$ ls -lt "foobar_??????.log"
. Which also wont work. -
zundarz over 6 years@tomas, I 'cat' the script at the top of the post.
-
-
roaima over 6 yearsIf the OP doesn't leave the variable unquoted it won't work as required. This is one of the few situations where quoting a variable is not the right thing to do. Also, I don't see anything that suggests the OP will be parsing
ls
. -
jesse_b over 6 years@roaima, me either but I doubt the whole purpose of the script is to list directory contents. I'm sure he eventually wants to act on this output.
-
jesse_b over 6 years@zundarz: I've updated my answer with some help on how to rename these files as well.
-
roaima over 6 years@zundars if you'd asked that question instead you would have been pointed to a set of answers to do exactly that. None of them would use
ls
.