Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-01-2013, 06:36 AM   #1
Registered: Feb 2007
Distribution: Debian
Posts: 84

Rep: Reputation: 15
Question Remove / from a string in a bash script

Hello people,

I know very little about bash scripting, but reading a couple of online manuals and forums I managed to create a script that gets the title of a youtube video and creates an empty file with the same name:

name=$(youtube-dl --get-title $1)
touch "$name"
exit 0
It works flawlessly but when there's a video whose title contains slashes / the file is not created. For example

How can I remove all the slashes from a title and replace them with a vertical bar | ?

Thank you in advance

Last edited by tauro_kpo; 02-01-2013 at 09:33 AM.
Old 02-01-2013, 06:49 AM   #2
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Originally Posted by tauro_kpo View Post
How can I remove all the slashes from a title and replace them with a vertical bar | ?
If it's a variable (e.g. your $name variable), you can do it with parameter substitution:

This is really "${name//REPLACE-THIS/WITH-THIS}".

See the advanced bash scripting guide.
1 members found this post helpful.
Old 02-01-2013, 08:28 AM   #3
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 3,545

Rep: Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884
Note - the "/" is a special character used to delimit directories from files. Normally the "|" is also a special character - it is the pipe symbol used to connect the standard output of one command (the one on the left) with the standard input of the next (the one on the right).

It IS possible to put a "|" into files, but it tends to cause a lot of problems with shell scripts that process lists of files.

The more frequent substition is either a _ (used for spaces) and "." (used for anything else).
1 members found this post helpful.
Old 02-01-2013, 09:32 AM   #4
Registered: Feb 2007
Distribution: Debian
Posts: 84

Original Poster
Rep: Reputation: 15
Thumbs up

That worked fantastic John, I'll save that link.

jpollard I'll keep your advice in mind.

Thank you both very much.


bash, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to remove string from all text files, recursively iacchi Programming 9 04-27-2012 06:22 AM
How to remove string in the text file ? Bash script dlugasx Linux - Server 9 06-05-2009 12:40 PM
help with bash script: remove * to the last . in string drkstr Linux - Software 3 04-25-2006 05:54 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 06:54 PM

All times are GMT -5. The time now is 03:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration