bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A bug when using bash shell variable


From: Andreas Kusalananda Kähäri
Subject: Re: A bug when using bash shell variable
Date: Thu, 13 Jan 2022 11:55:45 +0100

When you want to store several separate arguments, do not combine them
into a single string.

Compare this

        args="-a 'hello world' -b '1 * 3'"
        printf '"%s"\n' $args

and

        args=( -a 'hello world' -b '1 * 3' )
        printf '"%s"\n' "${args[@]}"


Using an unquoted variable causes the shell to split the variable's
value on spaces, tabs, and newlines (by default, the values in the IFS
variable).  The words generated from this also undergoes filename
globbing.  This is not what you want to do.

Instead, use an array to store the quoted arguments, and then make sure
that you properly quote the expansion of the array's elements when you
use it on the command line.

The bug is in your code, not in bash.

See also:

https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary
https://unix.stackexchange.com/questions/444946/how-can-we-run-a-command-stored-in-a-variable

(and others)


On Thu, Jan 13, 2022 at 10:13:05AM +0000, ju nan wrote:
> I hava run into some troubles when I use bash shell to execute qemu, here is 
> the log.
> 
> 
> 
>   1.  Run qemu with arguments directly in bash shell, I use strace to track 
> execve syscall, everything goes well
> 
> junan@u0:~/Documents/coding/run-riscv-qemu-linux$ strace qemu-system-riscv64 
> -nographic -machine virt -kernel ../linux-5.11/arch/riscv/boot/Image -append 
> "root=/dev/vda ro console=ttyS0" -drive file=root.ext4,format=raw,id=hd0 
> -device virtio-blk-device,drive=hd0
> execve("/usr/bin/qemu-system-riscv64", ["qemu-system-riscv64", "-nographic", 
> "-machine", "virt", "-kernel", "../linux-5.11/arch/riscv/boot/Im"..., 
> "-append", "root=/dev/vda ro console=ttyS0", "-drive", 
> "file=root.ext4,format=raw,id=hd0", "-device", 
> "virtio-blk-device,drive=hd0"], 0x7ffc39cda858 /* 58 vars */) = 0
> brk(NULL)                               = 0x55bb77e20000
> arch_prctl(0x3001 /* ARCH_??? */, 0x7ffd61474450) = -1 EINVAL (Invalid 
> argument)
> access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or 
> directory)
> openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
> 
> 
>   1.  But when I use bash variables to do it again, something wired happend, 
> as you can see, the argument "root=/dev/vda ro console=ttyS0" is splited into 
> different parts, so qemu did not start successfully 😋
> 
> 
> junan@u0:~/Documents/coding/run-riscv-qemu-linux$ echo $QEMU
> qemu-system-riscv64
> junan@u0:~/Documents/coding/run-riscv-qemu-linux$ echo $QOPTIONS
> -nographic -machine virt -kernel ../linux-5.11/arch/riscv/boot/Image -append 
> "root=/dev/vda ro console=ttyS0" -drive file=root.ext4,format=raw,id=hd0 
> -device virtio-blk-device,drive=hd0
> 
> junan@u0:~/Documents/coding/run-riscv-qemu-linux$ strace $QEMU $QOPTIONS
> execve("/usr/bin/qemu-system-riscv64", ["qemu-system-riscv64", "-nographic", 
> "-machine", "virt", "-kernel", "../linux-5.11/arch/riscv/boot/Im"..., 
> "-append", "\"root=/dev/vda", "ro", "console=ttyS0\"", "-drive", 
> "file=root.ext4,format=raw,id=hd0", "-device", 
> "virtio-blk-device,drive=hd0"], 0x7ffe1e71a928 /* 58 vars */) = 0
> brk(NULL)                               = 0x5613dfdc4000
> 
> 
> Thank you all for making so many excellent softwares, and I wish this will 
> help to improve the bash shell 😊.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]