Putting the results of grep & cut in a variable in a bash script

Here is a working version of your code with the changes commented as to why they were made.


function latest_file_name {
    local url="http://wordpress.org/latest.tar.gz"

    curl -s --head $url | # Add -s to remove progress information
    # This is the proper place to remove the carridge return.
    # There is a program called dos2unix that can be used as well.
    tr -d 'r'          | #dos2unix
    # You can combine the grep and cut as follows
    awk -F '=' '/^Content-Disposition/ {print $2}'

function main {
    local file_name=$(latest_file_name)

    # [[ uses bash builtin test functionality and is faster.
    if [[ -e "$file_name" ]]; then
        echo "File $file_name does already exist!"
        echo "There is no file named $file_name in this folder!"


Tags: , , , , , , , ,