简体   繁体   中英

Assign variable subdirectory to output command in the shell script

I'm trying to implement shell script to get the list of files of subdirectory:

#!/bin/bash
PATH="/Movies"
MOVIES=${PWD}${PATH}
LS="$(ls -lp) ${MOVIES}"

But I'm getting this error: ls: command not found

How can add the subdirectory to the ls command? I'll really appreciate your help.

in setting a PATH variable in your script, you have overwritten the default PATH variable that tells your shell where your common binaries are located. Either change your PATH variable name, or use the full ls path '/bin/ls'

#!/bin/bash
PATH="/Movies"
MOVIES=${PWD}${PATH}
LS="/bin/ls -lp ${MOVIES}"

$LS

or

#!/bin/bash
NOTPATH="/Movies"
MOVIES=${PWD}${NOTPATH}
LS="ls -lp ${MOVIES}"

$LS

more info here

It's also a good idea to not use UPPERCASE variables in shell scripts, as they're the default pattern for system wide environment variables. This way you will avoid overwriting some variable with your own. If you must, run the env command to see what variables are set in your bash session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM