• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

bash

Bash Global Variables

July 27, 2023

Just a few things about Bash variables.

Every variable in Bash is global and are accessible anywhere in the script.

They can be used in a function, loop, or conditional statement within the script.

# setting a variable
a = "hello"
# printing a variable
echo $a
# using a variable inside a function
hello_function () {
  echo $a
}
# passing a variable to a function
hello_function $a

# setting a variable a = "hello" # printing a variable echo $a # using a variable inside a function hello_function () { echo $a } # passing a variable to a function hello_function $a

Filed Under: Linux Tagged With: bash, function, global, variable

Bash Replace Spaces with a Comma

May 8, 2023

Replace spaces with commas from one file and send the output to another file.

tr -s '[:blank:]' ', ' < input.txt > output.txt

tr -s '[:blank:]' ', ' < input.txt > output.txt

It’s perfect for creating a comma delimited file for importing to Excel.

Filed Under: Linux Tagged With: bash, commas, file, input, output, replace, spaces

Bash Sort Files

April 10, 2023

How to sort files in Bash.

Display contents

$ cat file.txt
bbb
aaa
ccc
kkk
uuu
ppp

$ cat file.txt bbb aaa ccc kkk uuu ppp

Sort

$ sort file.txt
aaa
bbb
ccc
kkk
ppp
uuu

$ sort file.txt aaa bbb ccc kkk ppp uuu

Sort in place

$ sort -o file.txt file.txt
$ cat file.txt
aaa
bbb
ccc
kkk
ppp
uuu

$ sort -o file.txt file.txt $ cat file.txt aaa bbb ccc kkk ppp uuu

Filed Under: Linux Tagged With: bash, file, sort

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Interim pages omitted …
  • Go to page 16
  • Go to Next Page »
  • Home
  • About
  • Search

Copyright © 2023