编写Jupyter启动批处理脚本时,如何让%1为空而非返回‘ECHO is off.’?
Hey there! I see the issue you're running into—when you don't pass a path to your jn command, the %1 variable ends up empty, and echoing it shows "ECHO is off." instead of an empty value. Let's fix this while keeping @echo off enabled (since that's important for a clean command line experience).
Why This Happens
When no argument is provided, %1 is an empty string. When you run echo %user% (after setting user=%1), you're effectively running echo with no input. In @echo off mode, the command prompt responds by telling you "ECHO is off." instead of just showing a blank line.
The Fixes
We'll adjust two key parts of your script to handle empty arguments correctly and avoid that confusing message:
- Use
%~1for argument handling: This syntax automatically strips any surrounding quotes from the input argument (useful if someone passes a path with spaces in quotes) and properly returns an empty string when no argument is provided. - Quote your variables: When setting variables and echoing them, wrapping them in double quotes ensures empty values are handled gracefully, and paths with spaces don't break your script.
Modified Script
Here's your updated script with these fixes applied:
@echo off setlocal title Jupyter Notebook set "init=C:\Users\Sarthik\Machine Learning" set "user=%~1" echo User path: "%user%" if "%user%" == "" ( set "user=%init%" ) echo. echo Starting Jupyter Notebook at "%user%", Please Wait... echo. echo. cd /d "%user%" && (jupyter notebook) || ( echo. echo. echo ================== You have entered WRONG PATH ================== echo. pause )
Key Changes Explained
set "init=..."andset "user=...": Using quoted variable assignment prevents issues with spaces or special characters in your paths.%~1: Ensures empty arguments are treated as empty strings, and removes unnecessary quotes from user-provided paths.echo User path: "%user%": Wrapping%user%in quotes means even if it's empty, you'll seeUser path: ""instead of "ECHO is off."cd /d "%user%": Quoting the path here ensurescdworks correctly with paths that have spaces.
Now when you run jn without a path, the script will correctly detect the empty argument, fall back to your default init path, and start Jupyter Notebook there. If you pass a valid path, it'll use that instead—and if the path is invalid, it'll still show your error message as expected.
内容的提问来源于stack exchange,提问作者Sarthik Garg

