[TriLUG] Changing slashes to backslashes in prompt

David Both via TriLUG trilug at trilug.org
Fri Apr 19 16:14:16 EDT 2019


First - kudos to you for trying to learn Linux and the command line.

<rant>

I understand what you are doing and why, but the author of that book 
should never be allowed to write another book with the word Linux in it. 
Ignore that exercise once you have finished it. Never think of it again. 
Then find a better book.

There is a horrible problem with Windows people trying to make Linux 
look and work like Windows. In my articles and books I caution against 
this approach to learning Linux. You will never really learn Linux if 
you try to make it work like Windows. My primary example of this is 
using aliases so the user can type a Windows command that is then 
interpreted by the shell into its Linux equivalent. I will add this 
example to my list at the next opportunity.

What this tells me is that the author is stuck in the Windows world and 
is, probably unconsciously but nonetheless, trying to infect readers in 
the same way. Linux is not Windows, and trying to make it so will only 
impede your efforts to learn Linux.

I can think of a slew of different things to do with the command prompt 
than making it look like Windows.

</rant>

Good luck and keep learning!


On 4/19/19 3:55 PM, Bick via TriLUG wrote:
> I am reading through a Linux book right now and one of the exercises in the chapter about environment variables is to make the prompt look more Windows like.  Specifically, I am supposed to make the prompt display C:\directory.  I first  attempted to use sed to accomplish it like this: PS1='C:$(echo $(pwd) | sed 's:/:\\:g') '.  The result is C:parentDirchildDir.  It does not print slashes.  If I just put the command I am calling directly into the bash prompt, I get the proper output: C:\parentDir\childDir.
>
> I thought maybe the problem was that the single slash was being reinterpreted as an escape character when PS1 was being read, so I tried using four slashes (so that two would be present in the PS1 string).  Again typing this at the command prompt worked (output was C:\\parentDir\\childDir), but it didn't work as part of assigning it to PS1.
>
> I have also tried assigning the commands I want to run to a variable, $NEWPWD, I created in PROMPT_COMMAND.  I then used the variable in the PS1 assignment.  This didn't work either.
>
> I have successfully replaced the slashes with letters, numbers, and even an exclamation point (which needed to be escaped by a backslash) using ALL of the above methods.  Any ideas why this doesn't work with backslashes?
>
> Thanks,
>
> Chris

-- 

*********************************************************
David P. Both
*********************************************************
The value of any software lies in its usefulness
not in its price.

— Linus Torvalds
*********************************************************



More information about the TriLUG mailing list