1

I am writing a bash script that will get the user defined filenames and then use alsa to play that file:

#!/bin/bash
export PATH=$PATH:/home/pi/Documents/audio
read -p "Enter a filename: " filename
aplay $filename

Above is what I have tried after reading about how to include path in a script. But at the terminal prompt, after I do ./script.sh, it returns no such a file in the directory. I also tried source ./script.sh thinking that the environment is only changed in the subshell and it returns the same.

All I need is to include the path within this script and does not change the system path permanently.

I'd appreciate it if someone can point me to the right direction. Thank you!

0

2 Answers 2

1

PATH is used for finding programs, not data files. If you want $filename to be a file in the audio directory, you need to concatenate it explicitly.

#!/bin/bash
audiodir=/home/pi/Documents/audio
read -r -p "Enter a filename: " filename
aplay "$audiodir/$filename"
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, i did not know that. Thank you!
1

Try specifying the absolute path of the file that you're playing like this:

#!/bin/bash
AUDIO_PATH="/home/pi/Documents/audio"
read -p "Enter a filename: " filename
aplay "${AUDIO_PATH}/${filename}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.