"No such file or dir" when using bash to automate wallpaper selection - NixOS

39 views Asked by At
#! /usr/bin/env bash

Wallpaper_Dir="~/.config/hypr/Wallpapers/" #using full path in my code

chosen=$(ls $Wallpaper_Dir --ignore='Best Wallpapers.md' | rofi -dmenu -i)

swww img "$Wallpaper_Dir'$chosen'" # I added the extra quotes so that if the image name contains a space it is still able to work

this code returns this error:

Error: "failed to open image: No such file or directory (os error 2)"

in terms of bug testing I have tried to echo: swww img "$Wallpaper_Dir'$chosen'" and it returns swww img ~/.config/hypr/Wallpapers/'Chill Study.jpg' which looks fine, note that I use the full path not ~ in my code, wondering if anyone could shed some light as to why

1

There are 1 answers

0
Ed Morton On
~/.config/hypr/Wallpapers/'Chill Study.jpg'

from your comment is not the same thing as:

"~/.config/hypr/Wallpapers/'Chill Study.jpg'"

from your code (expanded from Wallpaper_Dir="~/.config/hypr/Wallpapers/" ... "$Wallpaper_Dir'$chosen'" and assuming $chosen has the value Chill Study.jpg)

In the first case, .../'Chill Study.jpg', the 's are quotes read by the shell so that the spaces are treated as literal characters in your file name, but in the second case, ".../'Chill Study.jpg'", the ' themselves are inside a double quoted string and so the 's as well as the spaces are treated as literal characters in your file name.

Please read https://mywiki.wooledge.org/Quotes to learn about quotes and there are other issues with your script too, some of which http://shellcheck.net could help you with.