Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

store the last part path of file in variable?

i have path for example :- $1=/path1/path2/path3

i want to store path3 in variable


how i can do it in :- 1-windows 2-unix

share|improve this question
up vote 5 down vote accepted

You can use either parameter expansion or the basename utility.


x=$(basename "$1")
share|improve this answer
Forgot the quotes, but otherwise yes. – Ignacio Vazquez-Abrams Oct 10 '10 at 14:44
Quotes are not necessary if the variable is the only thing coming from the assignment, i.e., $ is right after =. – Juliano Oct 10 '10 at 14:47
how i can make this in windows ??? – Mohammad AL-Rawabdeh Oct 10 '10 at 15:15
@Osama Since your question is tagged 'bash', I guess it is about bash, no? Bash is the same in both Windows and Unix. If you want to know how to do this in cmd.exe, then that is another completely different thing. – Juliano Oct 10 '10 at 16:10
@Osama install cygwin on Windows. Then you'll have a decent command interpreted along with most of the linux commands, including the bash interpreter. – ringø Oct 10 '10 at 16:15

In Windows (shell script):

set FILE=c:\temp\test.txt
for /f %%F in ("%FILE%") do set FILE=%%~nF%%~xF
echo %FILE%
share|improve this answer
In the "How to Format" instructions to the right of the input box, it says "indent code by 4 spaces". – Dennis Williamson Oct 11 '10 at 1:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.