Bash Notes - Popd/Pushd & Return Value

Popd/Pushd

These two commands are usually used to change the directoy.

  • Pushd

    • Move to a certain directory, and store the current directory at the top of a stack.
  • Popd

    • Read the directory from the stack, move to it, and then delete the directory from the stack.

The first time using Pushd, the current directory would be stored at the bottom of the stack since the stack is empty. If the command is used again, another directory would be stored on top of the first directory.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@echo off
Echo current directory:%cd%
pushd C:\Intel\Logs
Echo current directory:%cd%
pushd C:\Download
Echo current directory:%cd%
pushd C:\WINDOWS
Echo current directory:%cd%
popd
Echo current directory:%cd%
popd
Echo current directory:%cd%
popd
Echo current directory:%cd%

Result:


Return Value

If we want to get the return value of a command and store it in a variable, usually we use the following command:

1
for /f "delims=" %%returnvalue in ('command') do set variable=%%returnvalue

command represents the command which we want to get the return value from. variable is where we store the return value. returnvalue would be ultimately set to the return value of this command.

To retrieve the value, we could use the following command:

1
%variable%

Example:

1
2
3
4
5
6
7
pushd C:
cd Program Files (x86)\Microsoft Visual Studio\Installer
for /f "delims=" %%t in ('vswhere.exe -property catalog_productLineVersion') do set version=%%t
popd
cd ..\
call Lib\premake5\Premake5.exe vs%version%
PAUSE

Result:


Bash Notes - Popd/Pushd & Return Value
https://rigel.github.io/BashNote01/
Author
Rigel
Posted on
July 31, 2022
Licensed under