[Solved] echo {A..Z}

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Tapster
Level 2
Level 2
Posts: 57
Joined: Tue Dec 19, 2017 10:43 am

[Solved] echo {A..Z}

Post by Tapster »

I am trying to get this command to show on individual lines. I've tried using the \\n but it does not work or my syntax is off.

Code: Select all

echo {A..Z}
I'm looking for a return for the letters similar to this for numbers.

Code: Select all

seq 1 30
Thanks in advance. Any other thoughts or command recommendations greatly apprciated.
Taps
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: echo {A..Z}

Post by JoeFootball »

Tapster wrote: Thanks in advance. Any other thoughts or command recommendations greatly apprciated.
You're probably looking for something more elegant, but this appears to work ...

Code: Select all

echo -e 'A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ'
Tapster
Level 2
Level 2
Posts: 57
Joined: Tue Dec 19, 2017 10:43 am

Re: echo {A..Z}

Post by Tapster »

Joe:
Thanks for this and I can see that it does. I may be using the wrong terminology but am curious if there is not a way to put line breaks in a range of character values. I'm going to keep working on it but would appreciate anyone's thoughts if one cares to share.
Best to all.
Moonstone Man
Level 16
Level 16
Posts: 6054
Joined: Mon Aug 27, 2012 10:17 pm

Re: echo {A..Z}

Post by Moonstone Man »

Tapster wrote: Tue Jun 29, 2021 9:41 pm I may be using the wrong terminology but am curious if there is not a way to put line breaks in a range of character values.
If you plan on doing this in a script, you need a loop.

Code: Select all

#! /bin/bash
for letter in {A..Z}
do
    echo ${letter}
done

Code: Select all

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: echo {A..Z}

Post by Termy »

You're probably wanting this:

Code: Select all

printf '%s\n' {A..Z}
This works because `printf` (shell builtin) will reuse the identifier (IE: %s), if you've not got enough identifiers relative to the data the formatting field formats, as in this case. It's better you experiment with it than me trying to explain the oddness of it. Here's another example:

Code: Select all

$ printf 'This is a: %s.\n' test carrot bag
This is a test.
This is a carrot.
This is a bag.
I should also point out that you could make further use of brace expansion, here:

Code: Select all

$ printf '%s\n' 'This is a '{test,carrot,bag}.
This is a test.
This is a carrot.
This is a bag.
But that last one isn't so readable.
Last edited by Termy on Tue Jun 29, 2021 10:11 pm, edited 5 times in total.
I'm also Terminalforlife on GitHub.
Moonstone Man
Level 16
Level 16
Posts: 6054
Joined: Mon Aug 27, 2012 10:17 pm

Re: echo {A..Z}

Post by Moonstone Man »

Termy wrote: Tue Jun 29, 2021 9:59 pm You're probably wanting this:

Code: Select all

printf '%s\n' {A..Z}
Well, that's much nicer. Thank you.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: echo {A..Z}

Post by Termy »

:) No problem. It's a nifty trick.
I'm also Terminalforlife on GitHub.
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: echo {A..Z}

Post by JoeFootball »

Termy wrote: printf '%s\n' {A..Z}
+1. Well done. :)
Tapster
Level 2
Level 2
Posts: 57
Joined: Tue Dec 19, 2017 10:43 am

Re: echo {A..Z}

Post by Tapster »

Thanks and I'll mark this one answered.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: echo {A..Z}

Post by rene »

One using nothing but brace expansion:

Code: Select all

echo -e \\b{A..Z}\\n
The \b is due to backspacing over an otherwise auto-space only.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: echo {A..Z}

Post by Termy »

rene wrote: Wed Jun 30, 2021 5:55 am ...
I get an extra newline character at the end. Putting that newline character there as the suffix for the brace expansion was good thinking.
I'm also Terminalforlife on GitHub.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Solved] echo {A..Z}

Post by rene »

Yeah, me too; just noticed that a simple -n to echo gets rid of that: echo -ne \\b{A..Z}\\n.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [Solved] echo {A..Z}

Post by Termy »

rene wrote: Wed Jun 30, 2021 6:39 am Yeah, me too; just noticed that a simple -n to echo gets rid of that: echo -ne \\b{A..Z}\\n.
Ah, of course! Good thinking. I rarely use `echo`, so I keep forgetting it has those flags. :lol:
I'm also Terminalforlife on GitHub.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Solved] echo {A..Z}

Post by rene »

FWIW, the lousy backspace-thing has me feel the printf solution better.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [Solved] echo {A..Z}

Post by Termy »

Yeah, the `printf` method is a standard approach, but your way is clever. :)
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”