Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cutting a string into several lines in bash

Tags:

bash

cut

I want to take the path of the local directory and put each directory on the path in a different line. I've tried to do it using cut:

pwd | cut -f 1- -d\/ --output-delimiter=\n

but it doesn't change the '/'s into EOL, but puts n's instead. What am I doing wrong?

like image 429
SIMEL Avatar asked Nov 03 '10 15:11

SIMEL


People also ask

How do you break a long line in bash?

To split long commands into readable commands that span multiple lines, we need to use the backslash character (\). The backslash character instructs bash to read the commands that follow line by line until it encounters an EOL.

How do I continue a line in bash?

From the bash manual: The backslash character '\' may be used to remove any special meaning for the next character read and for line continuation. thanks.


1 Answers

This should do the trick

pwd | tr '/' '\n' 

If you don't want an empty line in the beginning (due to the initial /) you could do

pwd | cut -b2- | tr '/' '\n' 

Example:

#aioobe@r60:~/tmp/files$ pwd /home/aioobe/tmp/files #aioobe@r60:~/tmp/files$ pwd | cut -b2- | tr '/' '\n' home aioobe tmp files 
like image 192
aioobe Avatar answered Sep 20 '22 14:09

aioobe