Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, What am I missing? list=`ls *.txt` echo $list or what could give me an equivalent to this? Regards Kyle...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    2

    Bash Scripting Help


    Hi,

    What am I missing?

    list=`ls *.txt`
    echo $list

    or what could give me an equivalent to this?

    Regards
    Kyle

  2. #2
    Just Joined!
    Join Date
    Aug 2010
    Location
    Italy
    Posts
    35
    What do you want to achieve ?

    from command line these should do:
    The variable "list" get the contents of a ls *.txt
    echo show that content.

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    2

    oops

    er my first post seems to be unclear.

    When I run that command I get
    "ls: cannot access *.txt: No such file or directory"

    I want to put the result of "ls *.txt"
    in the variable $list but I can't seem to do it.

    Thank for the reply though.

    Kyle

    Never mind Found the error.
    I can to CD into a directory. I thought it would just take ~ .
    Last edited by krayons; 08-21-2010 at 10:24 PM. Reason: Found the solution :D

  4. #4
    Just Joined!
    Join Date
    Aug 2010
    Location
    Italy
    Posts
    35
    Well it's working, it's just that you don't have any .txt in the script where the script is run.

    On my pc

    # cat test.sh

    #!/bin/bash
    list=`ls *.txt`
    echo $list

    # bash test.sh
    bash test.sh
    test.txt robots_new.txt robots_old.txt

    All is working

  5. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    You're being a little inefficient. This code runs a command, stores its output into a variable, and then prints out the output. You can just run the command directly to get the same results:
    Code:
    #!/bin/bash
    
    ls *.txt
    Also, the error message you saw is what happens if there are no ".txt" files in the current directory. In that case, the shell does not expand "*.txt", and ls is told to look for a file called "*.txt". There obviously is none, so it can't do anything .

    If you always want it to ls your home directory, you can use the command:
    Code:
    ls ~/*.txt
    Otherwise, "ls" will default to the current directory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •