2

I have a shell script in my /bin directory that has a line in it:play applause.mp3. The file applause.mp3 is colocated in the same directory as the executable.

Yet the play command gives me the error play FAIL formats: can't open input file 'applause.mp3': No such file or directory.

The file plays fine if I open it directly from the /bin directory. It's obvious that the .mp3 file needs to reside somewhere other than the /bin directory. Where does bash go to look for a file?

1
  • It doesn't. If you just give it a file name, it expects it to be in the current directory. Feb 22 '16 at 0:01
0

Bash looks for commands to execute in directories specified in PATH environment variable.

However in your scenario bash already did its job and run the play command. applause.mp3 is passed as an argument. It is totally up to play command how it interprets it.

In a common scenario where a command takes a filename argument it will parse the path starting from the current directory (which is why it worked for you when you changed the directory to /bin before calling the script).

To avoid problems you should use an absolute path (starting with /) in your argument.

0

Programs don't look for user data files in the directory where the executable is located¹. They sometimes look for configuration files or static data files in the same directory (but it isn't very common). But user data and installed programs are hardly ever in the same directories — programs are usually in system directories where users aren't allowed to write, and users' files are usually under their home directories.

When you pass a file name to a program, it's interpreted as a file name in the current working directory. Every process has a current working directory, and it's part of the process's environment, not associated to a particular program. You normally start in your home directory, and you can use the shell command cd to go to another directory. Any program started from the shell inherits the shell's current directory.

When you run play applause.mp3, this looks for a file called applause.mp3 in the current directory. The directory where the play executable is located is irrelevant.

If you want to use a file name that works no matter what the current directory is, pass an absolute file name (beginning with a /). In a shell script or on the command line, you can use the abbreviation ~ at the beginning to refer to your home directory. For example play ~/music/applause.mp3 runs play on the file called applause.mp3 located in the subdirectory called music of your home directory.

¹ They can, but they hardly ever do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.