Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:07:16+00:00 2026-06-04T20:07:16+00:00

I want to have the same output as ls creates in a bash or

  • 0

I want to have the same output as ls creates in a bash or python script.
I don’t mean listing directories, but the thing ls does to be able to be “loopable”. Example:

# ls
a b c d

# ls | head -n 1
a

# for i in $(ls); do echo "file: $i"; done
file: a
file: b
file: c
file: d

How can ls be like that and still display everything in one line when calling it?

Using tabs doesn’t work.. Newlines just force it to be multiple lines. \000 doesn’t work.

# echo -e "a\tb\tc\td" | head -n 1
a    b    c    d

echo -e "a\000b\000c\000d" | head -n 1
abcd

cat -A doesn’t give me much info…

# cat -A <(ls --color=no)
a$
b$
c$
d$

# cat -A <(echo -e "a\nb\nc\nd")
a$
b$
c$
d$

So.. How can I generate the same type of output does in my scripts? Is there any control characters I am missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:07:18+00:00Added an answer on June 4, 2026 at 8:07 pm

    The trick is to detect whether the output is a terminal, in which case ls uses columns, or not, in which case it outputs in a simpler format.

    In Unix, you should be able to use Python’s os.isatty() function to get this information.

    From the shell, you can use the tty(1) program: tty -s <&1. This will exit true if stdout is a tty, and false if it is not. tty actually checks stdin, but with <&1 you can redirect stdout to stdin to effectively check stdout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the same standard options menu, but I want to change the background
James here. I'm working on a site that I want to have the same
Definitely a beginner question... basically, I want to have the same design from this
I have multiple images that have the same class attribute and I want to
I have several different lists I want to call. They all have the same
I want to have the <h2> underlined and blinking at the same time. Is
In Ruby, I want to have two threads running at the same time, and
I have a hierarchy of stuff I want to display (at the same time)
We have two version of the same assembly in GAC? I want my client
I have a millions of pairs of string of same length which I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.