This works from the terminal:
ls /dev/sda*
I want it in a bash script, using a variable. I tried:
device="a"
ls "/dev/sd"$device"*"
But I get the error: ls: cannot access '/dev/sda*': No such file or directory
.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityls /dev/sd$device* # or
ls "/dev/sd$device"*
You must not quote the globbing metacharacters if you want globbing to be performed.
ls /dev/sd"$device"*
would also be fine since /dev/sd/
doesn't contain any metacharacters.
May 26, 2018 at 20:31
ls
knows nothing about them.
May 26, 2018 at 21:52
device=sda*; ls /dev/$device
, and get glob expansion after the variable substitution.
The double quotes tell the shell to leave the *
as-is instead of expanding it. This will work:
device="a"
ls "/dev/sd${device}"*