Putting a single quote in a filename

7,228

Solution 1

You can't escape a single quote within single quotes. But you can juxtapose multiple quoted strings and they will be concatenated. So just use single quotes for the part not containing a single quote, then append a single quote escaped with a \, like this:

mkdir '$"dollars"&<>/dogs'\'

Solution 2

escape the last quote like so:

mkdir '$"dollars"&<>\dogs'\'
Share:
7,228

Related videos on Youtube

Mohamed Medhat Sallam
Author by

Mohamed Medhat Sallam

Linux for me is a to enjoy life. Not just a kernel. echo "Thank you Linus trovalds"

Updated on September 18, 2022

Comments

  • Mohamed Medhat Sallam
    Mohamed Medhat Sallam over 1 year

    We can a make a directory named $"dollars"&<>\dogs if we surround it with single quotes like the following

    mkdir '$"dollars"&<>\dogs'
    

    However, what if I want to include a single quote in the directory name?

    If I want my directory name to be $"dollars"&<>/dogs'

    Then How can I use the single quoting method do this ?

    I tried to do

    mkdir '$"dollars"&<>/dogs'' But this didn't work for and also

    mkdir ''$"dollars"&<>/dogs'' didn't work either.

    I even tried to put a backslash before it, but there was no use !

  • Mohamed Medhat Sallam
    Mohamed Medhat Sallam over 8 years
    Thanks but Why would this work ?
  • VaTo
    VaTo over 8 years
    You are escaping the last quote, and so you are telling bash to take it as part of the name of the directory not as part of the string syntax which is 'string'.