ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello! I am writing a program that reads a bunch of arguments from the command line,then read information from a file(passed as one of the arguments) and do some computation. The problem I am facing is when a backslash(\) is present as one of the arguments, suppose $ myprog \ abc xyz,the backslash is completely ignored, for the previous example $1 is "abc" where as it should be "\", I have tried using double-quotes("$1") but it still stays the same. The problem is the same when I am reading from a file the backslash character is completely ignored as if it were a space or a tab.
How can I prevent htis and read "\" as it is? Your help will be greatly appreciated.
The problem I am facing is when a backslash(\) is present as one of the arguments, suppose $ myprog \ abc xyz,the backslash is completely ignored, for the previous example $1 is "abc" where as it should be "\", I have tried using double-quotes("$1") but it still stays the same.
Are you trying to escape the space before "abc" or is the backslash a literal character. In your example, the backslash escapes the space character so the arguments are interpreted by the shell as " abc" "xyz". If it is a literal character, use either single quotes or a double backslash. Try to avoid slashes in filenames if possible. It is one of the evil characters that can cause problems. For example, if a filename can begin with a hyphen, you need to precede the arguments with a double hyphen so that the file isn't taken as an option. Exclaimation points are used by the shell to recall previous shell entries, but just when you are using an interactive shell.
Try to avoid slashes in filenames if possible. It is one of the evil characters that can cause problems. For example, if a filename can begin with a hyphen, you need to precede the arguments with a double hyphen so that the file isn't taken as an option. Exclaimation points are used by the shell to recall previous shell entries, but just when you are using an interactive shell.
Yes backslashes(\) are literal characters.
The problem is that the input for the filenames will be provided by different users, so my program should be ready to handle backslashes at all times. Furthermore, I am trying to read the arguments into local variables( var=$1) so I am not quite sure where to add the backslash character. I face this same problem with backslashes while reading them from a file, read completely ignores them as if they were just a tab or a space.
What does a backslash in the input represent? It better not be part of a filename, because windows uses backslashes to denote directories.
To enter a single argument starting with '\ ', either put it in single quotes or use:\\\<space>. The first two backslashes will be reduced to a single backslash. The third escapes the space. Be sure to use double quotes around the variables in the program, if a variable may have any special characters that bash will interpret specially.
Changing the IFS variable to \n is also used to prevent an argument from being broken up, but this will give you one argument containing all the arguments.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.