1

So far I managed to create a variable which stores the string and replaced blank with _ and : with @

title=$(youtube-dl --get-title https://www.youtube.com/watch?v=8b1JEDvenQU)
echo  "${title}"
echo  "${title}" | sed 's/ /_/g;s/:/@/g'

XGBoost Part 2: Classification
XGBoost_Part_2@_Classification

I want to assign mod_title -> ${title}" | sed 's/ /_/g;s/:/@/g'

and use this variable to use inside a function but I am not able to assign and print it.

This is what I have tried:

title=$(youtube-dl --get-title https://www.youtube.com/watch?v=8b1JEDvenQU)
mod_title = "${title}" | sed 's/ /_/g;s/:/@/g'
echo "${mod_title}"

error:

b.sh: line 2: mod_title: command not found

I am new to bash scripting. So no idea how to do it.

Pygirl
  • 113
  • 4

1 Answers1

1

Command substitution is what you used for your youtube-dl command ($( ... )). You need to use it again. Additionally in bash you cannot have spaces around the = for variable assignment:

title=$(youtube-dl --get-title https://www.youtube.com/watch?v=8b1JEDvenQU)
mod_title=$(echo "$title" | sed 's/ /_/g;s/:/@/g')
echo "$mod_title"

You could do this in one operation though:

title=$(youtube-dl --get-title https://www.youtube.com/watch?v=8b1JEDvenQU | sed 's/ /_/g;s/:/@/g')
jesse_b
  • 37,005
  • Can't accept the answer right now but will do it after 10 min. Thanks for providing the detailed answer . – Pygirl Dec 06 '20 at 18:28
  • 3
    tr for single character transliteration. sed is needlessly heavy for this. Or, just use ${variable//pattern/text} twice. – Kusalananda Dec 06 '20 at 18:31
  • @Kusalananda I will take a note of it. Thanks for pointing this out. – Pygirl Dec 06 '20 at 18:34