Molnia
Molnia

Reputation: 60

echo: "Why need to use \\ instead of \ to cancel the / character?"

~/Desktop $ echo */*
unix/junk unix/save unix/xyxxy

I would like to cancel the slash so the shell no longer prints the files of the directories. I've found out that

~/Desktop $ echo *\/*
unix/junk unix/save unix/xyxxy

doesn't work ,while

~/Desktop $ echo *\\/*
*\/* 

does the job.

Can someone explain why this is happening?

Upvotes: 0

Views: 60

Answers (4)

delephin
delephin

Reputation: 1115

If all you want is to print the folders within your Desktop then use echo */

echo *\\/* will print anything withing a folder that ends with \ while echo *\/* will print anything withing any folder as the backslash (\) is not scaped

Upvotes: 0

Steve Summit
Steve Summit

Reputation: 48000

Actually, what you're trying to cancel here is not the special behavior of the \ character, but rather, the special behavior of the two * characters!

Me, I would use

echo '*/*'

or

echo "*/*"

Or, if you wanted to use \, the best way to do it would be

echo \*/\*

Actually, since there are almost certainly no files or directories named "*", you would usually be able to get away with just

echo \*/*

or

echo */\*

When you wrote

echo *\\/*

you were asking to see all the names of all the files in any subdirectory where the subdirectory name ended in a \. There probably aren't any of those, but if you want to, to see what's going on, try invoking

mkdir x\\
touch x\\/y
touch x\\/z

and then do your

echo *\\/*

again. You should see x\/y x\/z!

Upvotes: 4

Arun Saragadam
Arun Saragadam

Reputation: 75

I didn't make myself clear with the earlier answer. What's happening with the last command

~/Desktop $ echo *\\/*
*\/*

is basically \ is escaping the next \ and *\/* is just getting printed like any other ordinary string like it happens with *something or any other string abc

Upvotes: -1

Michael Schmitt
Michael Schmitt

Reputation: 139

A single backslash is used for special sequences like for instance \n . So to insert a real - escaping - backslash, you have to write \\.

Upvotes: 0

Related Questions