Why does this Bash command work in a terminal but fail in a script?

November 13, 2026 4 answers
Asked by b7!rombank_265

I can run steamosctl get-default-desktop-session in a terminal, but the same command in a Bash script reports no such file or directory. The script has a Bash shebang and set -x. Running steamosctl help from it works.

Do I need to quote the argument because it contains several hyphens, or should I be looking elsewhere?

Answers

d612+awning_49

The hyphens inside get-default-desktop-session don't need special quoting. Bash passes that as one argument. Quoting the whole command and argument together would instead make Bash look for an executable with spaces in its name.

Look closely at the output from set -x and the full error. If the trace shows steamosctl starting, the missing file could be something the program tries to open, not the executable itself. The help command working doesn't prove the other operation has everything it needs.

Reply to this answer
8m~tallyboard_304

Compare the environment and working directory in both cases. In the script, you can add pwd and printenv, redirecting their output to a file if the script runs at boot. Check which user runs it too.

A noninteractive shell can have a different PATH, so try the full path to steamosctl. If that still fails, PATH isn't enough to explain it. The program may depend on the working directory, another executable, or environment variables. A service can set User and WorkingDirectory explicitly, but choose the directory the program actually needs rather than assuming /bin is always right.

Kettle.2Go:

Sourcing ~/.bashrc is another possible experiment, but it isn't a general fix. Some .bashrc files return early in noninteractive shells, and services shouldn't have to depend on interactive shell setup.

Reply to this answer
!recheck~the+teapot

The help command may just print usage, while the session query may need to contact a service over D-Bus. If this is running at boot, check whether the relevant user session and bus are available at that point.

As a diagnostic, try busctl --user introspect com.steampowered.SteamOSManager1 /com/steampowered/SteamOSManager1 in each context. Failure there could help narrow down a bus or service availability issue, though it won't by itself explain every missing-file error.

Reply to this answer
curbside-asterisk

Also check the actual bytes in the script, especially if the command was pasted from somewhere. cat -A path/to/script can reveal hidden characters, and od -A x -t x1z -v path/to/script gives a byte-level view. Look for carriage returns or a different dash character.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions