Bash - length of command line argument $0 [duplicate] - bash

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}

Related

How to save path to executable that includes space and argument as variable? [duplicate]

This question already has answers here:
Execute command containing quotes from shell variable [duplicate]
(2 answers)
Closed 1 year ago.
$ cat test.sh
#! /bin/bash
run="/Applications/YubiKey\ Manager.app/Contents/MacOS/ykman openpgp"
$run info
$ ./test.sh: line 5: /Applications/YubiKey\: No such file or directory
Is there a way to handle space in path to executable (/Applications/YubiKey\ Manager.app/Contents/MacOS/ykman) and space between path and argument (…/ykman openpgp)?
You need the quotes or the backslash, not both.
run="/Applications/YubiKey Manager.app/Contents/MacOS/ykman openpgp"
or
run=/Applications/YubiKey\ Manager.app/Contents/MacOS/ykman \openpgp
Update: as this is a command name and an argument, not a single path name, you should be using either an array:
run=("/Applications/YubiKey Manager.app/Contents/MacOS/ykman" openpgp)
"${run[#]}" info
or better yet, define a function instead of a variable:
run () {
"/Applications/YubiKey Manager.app/Contents/MacOS/ykman" openpgp "$1"
}
run info

Command substitution with variables gives command not found? [duplicate]

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.

Running a command line through shell script is not working [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I have an application in Unix. I use the below command to connect to it:
./application -a "connect"
I want to do the same through the shell script, for which i assigned the command line to a variable like:
newcommand = './application -a "connect"'
$newcommand
But this is not working.
However the first part of the code is working. i.e.,:
newcommand = "./application"
$newcommand
Can anyone point out what i am missing.
Believe it or not, this:
newcommand = "./application"
...has the shell run the command, newcommand with the arguments, =, and ./application.
In shell simple assignments cannot have any unprotected whitespace or they'll be interpreted as a command.
Consider:
newcommand=./application
$newcommand
...notice that there's no space around the = sign in the assignment.

how to write bch commands in bash file [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 4 years ago.
I want to write a bash script to get block count . It is giving error
./script.sh: line 4: =: command not found
Below is my script
#!/bin/bash
# getblockcount
$blockcount = bitcoin-cli getblockcount
echo $blockcount
Kindly tell what is wrong .
There should not be space around the operator.. So remove the space around = and thing should work. Also there are some other bits.. Here is the corrected one.. Ensure command bitcoin-cli getblockcount from terminal gives right result.
#!/bin/bash
# getblockcount
blockcount=$(bitcoin-cli getblockcount)
echo $blockcount

Bash variable assignment and command not found [duplicate]

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"

Resources