Solved

special characters in linux - when is \t a tab or the time

Posted on 2011-03-05
3
527 Views
Last Modified: 2012-05-11
Hi

I'm not sure in linux how the difference between special characters work. When is \t a tab or the time. If i wanted to output a tab i could write

echo $'\t'

but if i wanted to set the shell prompt to show the time i would write

PS1='\t'

Why are they treated differently when both times i am creating a string?

thanks
0
Comment
Question by:andieje
3 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 35045780
which shell are you using?
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 250 total points
ID: 35045783
This is all about bash(shell) and how it handles the special characters.

You can
echo "\t" # displays \t
echo -e "\t" # displays tab
but PS1='\t' # uses time in prompt

So again, You always have to refer manual/documentation to find how an application handles special or escaped characters.
There's no such thing like \t always means tabulator.
0
 
LVL 22

Assisted Solution

by:blu
blu earned 250 total points
ID: 35046357
It depends on the context. There are no "universal" special characters. The C programming language defines a number of special characters that have become sort of defacto standards, primarily because much of the programs involved in the OS are written in either C or C++. But those special characters in C are defined only for character constants in programs and for certain output routines. It doesn't happen automatically in any string.

So, how special characters are handled on input depends on the program getting the input (also on the tty device driver if input is from a terminal). So, in this case the characters "\" and "t" are both actually stored in the string input. In the case of the bash shell, it is assigned to the PS1 variable as is, with no translation. Then, as part of the shell output routine, when it sees the "\t" in the prompt variable, instead of outputing the "\t", it outputs the current time.

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now