I'm trying to utilize dmenu for selecting from a list of my favorite applications, which are stored in a file located at ~/prj/dmenus/favorites/a.txt. When I run the command `$(cat ~/prj/dmenus/favorites/a.txt | dmenu)`, it works perfectly. However, I'm looking to enhance the output's readability with specific formatting options, like those found in the command `$(cat ~/prj/dmenus/favorites/a.txt | dmenu -fn 'Droid Sans Mono-18')`, which gives a better output.
The next step for me is to save the formatting options in a separate file, which I can read into a variable using another command substitution. I can successfully do this with the command `x=$(<~/.config/dmenu/layout.txt); echo $x`, and it outputs `-fn 'Droid Sans Mono-18'`.
However, when I try to combine this into one command like `$(cat ~/prj/dmenus/favorites/a.txt | dmenu $x)`, it gives me usage errors instead of executing correctly. I've also tried `$(cat ~/prj/dmenus/favorites/a.txt | dmenu $(<~/.config/dmenu/layout.txt))` but it similarly fails. I'm sure there's a simple solution to this that I'm missing. I'm using Ubuntu 24.04 with Fluxbox, and any help would be greatly appreciated!
5 Answers
Just a side note: if you're defining formatting options as variables, consider using the `${FONT+ -fn "$FONT"}` syntax. It allows for more flexibility, ensuring that it either expands correctly with the defined font or does nothing if it's not set.
Thanks to the other responses here! Each has given me some insight. I'm still curious about the use of eval—what specific risks are associated with it? I’d love to understand the potential dangers more.
The issue you're encountering has to do with how command substitutions handle quotes. When using variables within commands, they don't strip out the quotes as you might expect. Instead of trying to run dmenu with arguments from a file directly, it's often cleaner to write a small wrapper script that specifies everything, like `my-dmenu ~/prj/dmenus/favorites/a.txt`. This keeps things straightforward and avoids complicated substitutions, which can be a headache to maintain.
I think you might want to be careful with using eval for this. While it can help run commands from a config file, it opens the risk of executing unintended shell commands if the file content is altered. Always prefer safer ways if you can, like the wrapper script approach!
Thanks for the heads up! I appreciate the caution. What would your recommended method be for directly passing arguments in a safer way?