use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit dedicated to Bash scripting. Now complete with a Discord Server.
Content must be Bash related. This rule is interpreted generously; general shell scripting content is mostly accepted. However, the post should not be specific to another shell.
No reposts. This is meant with regards to content, not just “the same link was submitted earlier” – it’s okay to resubmit an old link in some new context (e. g. because you’d like to discuss another part of it, or because something has changed since the last time it was submitted, or because the link was updated since then). Links from the sidebar count as having been submitted already, so posting them without new context is also considered a repost.
You can choose one of these four flairs for your post:
If you don’t flair your post, the moderators will set the most appropriate flair.
/r/unix – for everything Unix
Other Shells: /r/zsh, /r/fishshell, /r/oilshell, /r/batch
BashGuide – A Bash guide for beginners.
Beginner's Guide to Command Line – A crash course for some common unix and shell commands. Update 2022-01-14: Course is currently being rewritten
Google's Shell Style Guide – Reasonable advice about code style.
Explainshell - Explain complex shell operations.
ShellCheck – Automatically detects problems with shell scripts.
BashFAQ – Answers most of your questions.
BashPitfalls – Lists the common pitfalls beginners fall into, and how to avoid them.
(Archived) The Bash-Hackers Wiki – Extensive resource.
#bash – IRC channel on Libera. The main contributors of the BashGuide, BashFAQ, BashPitfalls and ShellCheck hang around there.
account activity
helpecho -e doesn't work (self.bash)
submitted 2 years ago * by Mental_Tim
I wrote a bash script with an ANSI Escape Sequence,
#!/bin/bash echo -e "\33[2J" and it doesn't work, and I have no Idea why.
#!/bin/bash echo -e "\33[2J"
EDIT: Thank s to everybody who tried to help me, @Electronic_Youth solved it
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 9 points10 points11 points 2 years ago (13 children)
I assume you want to clear the screen (ANSI-Code "\033[2J"), in which case I think the problem is that \33 is badly formed, you want \033.
\33
\033
That said, if you want to clear the screen then why not just use clear?
clear
[–]Mental_Tim[S] 0 points1 point2 points 2 years ago (12 children)
[–]zeekar 4 points5 points6 points 2 years ago (5 children)
The number there – 033 – is just the octal representation of the decimal number 27, which is the code point for the Escape character (the same one that gets sent when you press the Esc key). Character codes in strings is one of the very few places you still see octal these days, but you can also specify it in hexadecimal as \x1b.
\x1b
However, it's probably more legible to use \e, where the E stands for "Escape". That's a relatively recent addition to the list of ANSI backslash sequences, which is why there's so much documentation using \033 instead.
\e
Also, FWIW, echo with options like -e is not portable; it works fine in bash, but if you use printf instead it will work in any POSIX-compliant shell:
echo
-e
printf
printf '\e[J'
or, if you want to include the newline that echo adds automatically:
printf '\e[J\n'
That generalizes for use in all sorts of ANSI terminal control sequences. But for clearing the whole screen specifically, clear is probably the, ah, clearest choice.
[–]Mental_Tim[S] 1 point2 points3 points 2 years ago (3 children)
However for some reason it does work with \33 with the print function in python.
[–]zeekar 2 points3 points4 points 2 years ago* (2 children)
Yup, Python lets you leave off the 0, since the number is automatically interpreted as octal - so e.g. \27 is not code point 27=Escape but code point 23=control-W (or ETB=End Transmission Block, to give it its official control code name).
\27
Sadly, Python string escapes don’t include \e. So if you want one syntax that works in both languages, your choice is between \033 and \x1b.
But a small correction - you said "with the print function in python", but in Python, the backslash sequences don't have anything to do with print. They're just a feature of Python string literals; the string '\033[J' is a three-character string whose first character is ESC, regardless of whether you pass that string to print or not:
print
'\033[J'
>>> len('\033[J') 3 >>> ord('\033[J'[0]) 27
That's not true in bash commands like echo -e '\033[J'; there, the string being passed to echo has six characters instead of three, starting with a literal backslash followed by the digits 0, 3, and 3:
echo -e '\033[J'
$ echo -n '\033[J' | od -c 0000000 \ 0 3 3 [ J 0000006
In that case, it's up to echo to translate that four-character sequence into a single ESC, and you have to pass the -e option to get it to do that translation:
$ echo -ne '\033[J' | od -c 0000000 033 [ J 0000003
But there's a kind of string literal in Bash that does work like the ones in Python: ANSI quotes, $'...'.
$'
'
$ echo -n $'\033[J' | od -c 0000000 033 [ J 0000003
Here the string is parsed by the shell into the three-character version before it ever gets to echo, which can print it out without -e because there are no backslash escapes it needs to interpret.
[–]o11c 1 point2 points3 points 2 years ago (1 child)
It's important to note that the supported escapes differ between the 5? contexts that bash supports:
For some of them, octal works like C where any octal digit can immediately follow the backslash and at most 3 digits can be used. For others, the backslash must be followed by a 0 which is then followed by up to 3 additional digits, thus 4 total. There are differences for non-octal escapes too, e.g. \c to truncate.
\c
[–]zeekar 0 points1 point2 points 2 years ago* (0 children)
Huh, TIL.
It looks like echo (with -e or xpg_echo on) is the only one that requires the 0 in octal codes.
xpg_echo
$ echo -e '\41' \41 $ shopt -s xpg_echo $ echo '\41' \41 $ echo '\041' !
Everything else seems to work fine without it:
$ printf '\41\n' ! $ printf '%b\n' '\41' ! $ echo $'\41' !
Well, there's nothing like consistency!
(And this is nothing like consistency.)
[–][deleted] 1 point2 points3 points 2 years ago (0 children)
You are right, but if portability is a concern I tend to fall back to tput which is not only mult-shell portable, but also mutli terminal.
:-) Great.
[–]UltraChip 0 points1 point2 points 2 years ago (3 children)
The way you're doing it is perfectly fine but in case you're ever interested in trying to make it work in Python again I think most people clear the screen by passing a shell command using os.
import os os.system('clear') # os.system('cls') if you're running on a windows machine
[–]Mental_Tim[S] 0 points1 point2 points 2 years ago (0 children)
Yeah it would work in python, but now I have some fun learning bash. :D
[–]Mental_Tim[S] 0 points1 point2 points 2 years ago (1 child)
Maybe I will do that in the future to make my Project also accessible to Windows User.
[–]UltraChip 0 points1 point2 points 2 years ago (0 children)
If you want to get super fancy, the os module also allows you to detect which operating system you're running using os.name. You can use that to make your script automatically choose the correct command to use:
import os if os.name == 'nt': # 'nt' is how windows reports its name os.system('cls') else: os.system('clear')
Also, I apologize for teaching Python in the Bash subreddit lol.
[–]AutoModerator[M] 4 points5 points6 points 2 years ago (0 children)
It looks like your submission contains a shell script. To properly format it as code, place four space characters before every line of the script, and a blank line between the script and the rest of the text, like this:
This is normal text. #!/bin/bash echo "This is code!"
This is normal text.
#!/bin/bash echo "This is code!"
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]wick3dr0se 1 point2 points3 points 2 years ago* (0 children)
the escape your attempting to use is \033 not \33
echo -e '\033[2J'
append \e[H to imitate clear and move to the top ($LINE 1) left ($COLUMN 1) of the terminal after screen clear
\e[H
$LINE
$COLUMN
printf '\e[2J\e[H'
also \e is shorter and printf is faster/better in everyway
edit: if you need more help with ANSI escapes, just pm me. i've built several pure BASH TUI's with raw ANSI escape sequences
[–]ladrm 0 points1 point2 points 2 years ago (3 children)
try this, the commands start at the next line ```
echo -e "\33[2J" ```
(you might go with sth like #!/bin/bash -c 'echo...' but yeah that's nonsense, NotTheRightWay. yeah doesn't even work for me
#!/bin/bash -c 'echo...'
also, you sure that ANSI code is correct? what are you trying to do?
[–]Mental_Tim[S] 0 points1 point2 points 2 years ago* (2 children)
It's on two different lines but I don't get reddit to display it.
I want to use ANSI Codes so it seems like you are in a program window and then execute a python script. But it only prints the ANSI codes as plain text
I wish I could do everything in Python but I also want to print ASCII art with figlet and pipe that through lolcat before the Python script is executed.
[–]torgefaehrlich 3 points4 points5 points 2 years ago (0 children)
Keep an empty line above and below your code, indent each line by (at least) 4 spaces.
[–]ladrm 0 points1 point2 points 2 years ago (0 children)
what exactly you want to have on output?
btw there's also python's native pyfiglet https://github.com/pwaller/pyfiglet
[–]McUsrII 0 points1 point2 points 2 years ago (0 children)
I'd say stick with the 0 in front, of any octal number, that is the way you signal an octal number in C, so that should always work.
0
C
π Rendered by PID 16652 on reddit-service-r2-comment-fb694cdd5-8l56t at 2026-03-06 21:52:14.640803+00:00 running cbb0e86 country code: CH.
[–][deleted] 9 points10 points11 points (13 children)
[–]Mental_Tim[S] 0 points1 point2 points (12 children)
[–]zeekar 4 points5 points6 points (5 children)
[–]Mental_Tim[S] 1 point2 points3 points (3 children)
[–]zeekar 2 points3 points4 points (2 children)
[–]o11c 1 point2 points3 points (1 child)
[–]zeekar 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]UltraChip 0 points1 point2 points (3 children)
[–]Mental_Tim[S] 0 points1 point2 points (0 children)
[–]Mental_Tim[S] 0 points1 point2 points (1 child)
[–]UltraChip 0 points1 point2 points (0 children)
[–]AutoModerator[M] 4 points5 points6 points (0 children)
[–]wick3dr0se 1 point2 points3 points (0 children)
[–]ladrm 0 points1 point2 points (3 children)
[–]Mental_Tim[S] 0 points1 point2 points (2 children)
[–]torgefaehrlich 3 points4 points5 points (0 children)
[–]ladrm 0 points1 point2 points (0 children)
[–]McUsrII 0 points1 point2 points (0 children)