I want to assign the path and file name to a variable:
/path/to/myfile/file.txt
For example
MYFILE=$(pwd)$(basename)
How can i do it ?
I want to assign the path and file name to a variable:
/path/to/myfile/file.txt
For example
MYFILE=$(pwd)$(basename)
How can i do it ?
To answer the question as it is stated: This is a simple string concatenation.
somedirpath='/some/path' # for example $PWD or $(pwd)
somefilepath='/the/path/to/file.txt'
newfilepath="$somedirpath"/"$( basename "$somefilepath" )"
You most likely would want to include a /
between the two path elements when concatenating the strings, and basename
takes an argument which is a path (this was missing in the question).
Reading your other answer, it looks like you are looking for the bash
script path and name. This is available in BASH_SOURCE
, which is an array. It's only element (unless you are in a function) will be what you want. In the general case, it's the last element in the array that you want to look at.
In bash
4.4, this is ${BASH_SOURCE[-1]}
.
basename "$PATH"
and $( basename "$PATH" )
But still getting the error , tried in 2 differents env.
– Omar BISTAMI
Feb 09 '18 at 16:51
PATH
is a very special variable used by the shell to look up external utilities. Please use almost any other variable name. Normally, one's own scripts should use lower-case variable names so that they don't accidentally clash with system environment variables.
– Kusalananda
Feb 09 '18 at 17:08
I tried this and it worked :
#!/bin/bash
SCRIPT=$(pwd)$(basename $0)
echo $SCRIPT
If there is a better way, please share.