This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 3 years ago.
I'm having a BASH script:
#!/usr/bin/env bash
PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
YAML=$(envsubst < ${PATH}/test.yml)
echo "${YAML}"
It results:
./test.sh: line 5: envsubst: command not found
I see that having a variable inside command substitution causes such error.
If I'm not using any:
YAML=$(envsubst < ./test.yml)
Then I'm having an expected successful script execution.
I've tried different syntax using quotes all over the place, but nothing helped.
How do I successfully use variable inside command substitution?
Bash version:
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
UPDATE: A duplicate question. Found an answer here:
https://stackoverflow.com/a/5642584/5935309
The problem is that you are changing PATH, which is used by Bash internally to define where to look for programs (and in which order).
You changed PATH to only contain the current working dir, and that's not where envsubst is located.
The solution is to use something different than PATH, like FILE_PATH.
Related
This question already has an answer here:
How can I create a bash environment variable that prefixes an environment variable before a command?
(1 answer)
Closed 1 year ago.
On Bash, I first define a variable CMD for a command line bash instruction, then I run it. An error occurs. Where does it go wrong?
$ CMD="VERBOSE=1 ./myscript"
$ $CMD
bash: VERBOSE=1: command not found
Don't store commands in variables. Variables aren't smart enough to hold commands. They will let you down time and time again like your roommate that never washes the dishes.
Use a function. They're the right tool for the job. You can use them like they were regular executables; they can take arguments; there are no quoting/backslash/whitespace issues.
$ cmd() {
> VERBOSE=1 ./myscript
> }
$ cmd
Functions are where it's at.
See also:
How can I create a bash environment variable that prefixes an environment variable before a command?
This question already has answers here:
How to create shell variable with dashes?
(2 answers)
Closed 1 year ago.
I want to find the length of $0
If I use the following bash code
leading-space=${#0}
I get the following result:
./Install: line 25: leading-space=9: command not found
$0 has the value ¨./Install¨
The length of the string appears to be correct, but then bash gets confused. I am running bash 5.0.17 on Ubuntu 20.10. Can anyone see what I am doing wrong?
Identifiers in bash may not contain -, therefore the whole assignment is interpreted as a command (like cd, or grep) but there is no command with the name leading-space=9 on your system, resulting in the error.
Use the following:
leading_space=${#0}
This question already has an answer here:
Syntax error in sh - built and working for bash [duplicate]
(1 answer)
Closed 5 years ago.
I have the following script
list=\(\)
val=123
list+=\(\"$val\"\)
When I copy-paste it to the terminal it works fine, but when I try to run it from inside a .sh file, I get this error:
file.sh: 9: file.sh: list+=("123"): not found
I found this answer here, but it doesn't seem applicable since I'm not using a space.
btw, I use \ in front of ( because otherwise I get Syntax error: "(" unexpected, this also happend only when running from a script file.
What causes the error?
I'm working on GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
You are assigning the literal string () to list, not implicitly declaring it as an empty array.
list=()
val=123
list+=("$val")
This question already has answers here:
How do create an alias in shell scripts? [duplicate]
(3 answers)
Closed 5 years ago.
I have a simple bash script.
alias myls=ls
myls
If I execute this script, I get an error.
$ bash foo.sh
foo.sh: line 2: myls: command not found
Why does the alias not work in the script?
Does this behavior conform to POSIX?
If it is indeed not supposed to work, could you please point me to an authoritative documentation that stays this?
See man bash:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
I have a shell script that will let me access global variables inside the script, but when I try to create my own, it responds with: command not found.
#!/bin/bash
J = 4
FACE_NAME = "eig$J.face"
USER_DB_NAME = "base$J.user"
When I run the above script I get:
./test1.sh line 2: J: command not found
./test1.sh line 3: FACE_NAME: command not found
./test1.sh line 4: USER_DB_NAME: command not found
Any ideas?? I'm using Cygwin under Windows XP.
Try this (notice I have removed the spaces from either side of the =):
#!/bin/bash
J="4"
FACE_NAME="eig$J.face"
USER_DB_NAME="base$J.user"
Bash doesn't like spaces when you declare variables - also it is best to make every value quoted (but this isn't as essential).
It's a good idea to use braces to separate the variable name when you are embedding a variable in other text:
#!/bin/bash
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"
The dot does the job here for you but if there was some other character there, it might be interpreted as part of the variable name.
dont' leave spaces between "="
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"