Getting Current Path in Variable and Using It

Getting current path in variable and using it


myvar="$PWD"
cd "$myvar"

(Quotes are necessary if your path contains whitespaces.)

Save current directory in variable using Bash?

This saves the absolute path of the current working directory to the variable cwd:

cwd=$(pwd)

In your case you can just do:

export PATH=$PATH:$(pwd)+somethingelse

Windows shell command to get the full path to the current directory?

Use cd with no arguments if you're using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).

How to store current directory path in variable from bash script for windows?

Assuming you're really in bash:

mkdir upload
cd upload
DB_IMG_PATH=$PWD
echo "$DB_IMG_PATH"
cd ..

echo '{"public":{"imgUploadUrl":"'"$DB_IMAGE_PATH"'","adminUser":"admin@mitch.org","adminPassword":"admin@123"}}' >> settings.json

And the same thing without unnecessary cd steps:

mkdir upload
DB_IMG_PATH=$PWD/upload
echo "$DB_IMG_PATH"

Please be aware though:

  • By convention, we capitalize environment variables (PAGER, EDITOR, ..) and internal shell variables (SHELL, BASH_VERSION, ..). All other variable names should be lower case. Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables.
  • Never change directories in a script unless you check if it failed! cd $foo is bad. cd "$foo" || exit is good.

batch script to set a variable with the current path location

The current directory is in the "shadow" variable cd.

You could try

set "var=%cd%"

In Python Use Current Directory as variable

You have:

dir = os.getcwd()
timeStamp = datetime.datetime.now().strftime("%A, %d. %B %Y %I-%M%p")
os.path.join('/dir', 'video_'+timeStamp+'_.mov')

and seem to be puzzled as to why you are getting something like /dir/video… out of the os.path.join(). You have quoted /dir so are literally getting '/dir' in your pathname. If you instead used:

os.path.join(dir, 'video…')

Youd get the cwd concatenated with the rest of the file name.

What is the current directory in a batch file?

From within your batch file:

  • %cd% refers to the current working directory (variable)
  • %~dp0 refers to the full path to the batch file's directory (static)
  • %~dpnx0 and %~f0 both refer to the full path to the batch directory and file name (static).

See also: What does %~dp0 mean, and how does it work?



Related Topics



Leave a reply



Submit