Skip to content
Advertisement

How to get unix style prompt in windows (batch)

I am trying to make dos shell which displayes a unix/linux promt. The code is:

@echo off
:hi
set tmpdrv=%cd:~0,2%

if %homedrive% == %tmpdrv% (
  set drvmatch=1
) else (
  set drvmatch=0
)
set "MYSTRING=abcdef!%%^^()^!"
set MYSTRING=%homepath%
:strlen
(echo "%MYSTRING%" & echo.) | findstr /O . | more +1 | (set /P RESULT= & call exit /B %%RESULT%%)
set /A STRLENGTH=%ERRORLEVEL%-5
set tmppath1=%cd:~2%
CALL set tmppath2=%%tmppath1:~0,%STRLENGTH%%%
if %homepath% == %tmppath2% (
  set homematch=1
) else (
  set homematch=0
)

set homepathmatch=0
if %homepath% == %tmppath2% (
    if %homedrive% == %tmpdrv% (
    set homepathmatch=1
    )
)

if %homepathmatch%==1 (
    REM set /a STRLENGTH=%STRLENGTH%+2
        CALL set newpath=%%tmppath1:~%STRLENGTH%%%
    set newpath1=~%newpath%
)
set newpath2=%newpath1:=/%
:check_Permissions

 net session >nul 2>&1
 if %errorLevel% == 0 (
 set "username2=root"
 ) else (
 set username2=%username%
 )
set /p %command%="%username2%@%computername%:%newpath2%$ "
%command%
goto :hi

I want it to display directory name like in unix/linux but it displays a ~. What is wrong in my code? Can you please help me improve it?

Advertisement

Answer

Probably not quite on the ball with the prompts specific situational substring modification, but the Method of setting the Prompt is.

@ECHO OFF & Setlocal EnableDelayedExpansion & TITLE %~n0
cls & MODE 1000 & CD %userprofile%desktop


    For /F "tokens=* Delims=" %%A In ("%userprofile%") Do (
        Set "C_Prompt=%%A"
        Set "C_Prompt=!C_Prompt:=!"
        Set "C_Prompt=!C_Prompt:Users=!"
    For %%A in (a,b,c,d,e,f,g,h,i) Do (Set "C_Prompt=!C_Prompt:%%A:=!")
    )
    Set DirPath=:!CD:=/!
    For %%A in (a,b,c,d,e,f,g,h,i) Do (Set "DirPath=!DirPath:%%A:=!")
    Set "C_Prompt=!C_Prompt!@!computername!!DirPath! $$ "

prompt !C_Prompt!

Start /B cmd.exe

The above does not refresh the command prompt with the working directory, however will result in commands behaving as expected without expansion issues.

This linked Macro will refresh the Prompt when the working Directory changes, however the act of expanding commands within a user defined variable can result in unexpected results.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement