Robert
Robert

Reputation: 109

How to set a Directory as an Argument in Bash

I am having trouble finding out how to set a directory as an argument in bash.

The directory I am trying to have as an argument is /home/rrodriguez/Documents/one.

Anywhere I try to look for an answer I see examples like dir = $1 but I cant seem to find an explanation of what this means or how to set it up so that it references my specific file location. Could anyone show me how to set up my variable for my path directory?

Adding my code for a better understanding of what im trying to do:

#!bin/bash

$1 == 'home/rrodriguez/Documents/one/'

dir = $1

touch -c $dir/*
ls -la $dir
wc$dir/* 

Upvotes: 9

Views: 18247

Answers (1)

John1024
John1024

Reputation: 113814

Consider:

#!bin/bash

dir=$1
touch -c "$dir"/*
ls -la "$dir"

This script takes one argument, a directory name, and touches files in that directory and then displays a directory listing. You can run it via:

bash script.sh 'home/rrodriguez/Documents/one/'

Since home/rrodriguez/Documents/one/ is the first argument to the script, it is assigned to $1 in the script.

Notes

In shell, never put spaces on either side of the = in an assignment.

I omitted the line wc$dir/* because it wasn't clear to me what the purpose of it was.

I put double-quotes around $dir to prevent the shell from, among other things, performing word-splitting. This would matter if dir contains spaces.

Upvotes: 7

Related Questions