3 Added $[...] and head improvements suggested by sch
source | link

bash, sed and coreutils, 9595 89 bytes

You can define a function like this

f(){ n=$(($1n=$[$1-2));yes2];yes \ |head|sed -n$(($2*n))|tr$[$2*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
  n=$(($1-2))

  # The next couple of lines create a rectangle of spaces
  # matching the desired size
  yes ' '          |
  head -n$(($2*n)) |
  tr -d '\n'       |
  fold -w$n        |

  # Add the pipes and dashes
  sed '
    s/^\|$/|/g   # Replace first and last character by a pipe
    1! {$!b }    # Do nothing if not on first or last line
    s/ /-/g      # Add the dashes
  '
  echo
}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.

bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$(($1-2));yes \ |head -n$(($2*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
  n=$(($1-2))

  # The next couple of lines create a rectangle of spaces
  # matching the desired size
  yes ' '          |
  head -n$(($2*n)) |
  tr -d '\n'       |
  fold -w$n        |

  # Add the pipes and dashes
  sed '
    s/^\|$/|/g   # Replace first and last character by a pipe
    1! {$!b }    # Do nothing if not on first or last line
    s/ /-/g      # Add the dashes
  '
  echo
}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.

bash, sed and coreutils, 95 89 bytes

You can define a function like this

f(){ n=$[$1-2];yes \ |sed $[$2*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
  n=$(($1-2))

  # The next couple of lines create a rectangle of spaces
  # matching the desired size
  yes ' '          |
  head -n$(($2*n)) |
  tr -d '\n'       |
  fold -w$n        |

  # Add the pipes and dashes
  sed '
    s/^\|$/|/g   # Replace first and last character by a pipe
    1! {$!b }    # Do nothing if not on first or last line
    s/ /-/g      # Add the dashes
  '
  echo
}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.

2 Added a commented version of the answer
source | link

bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$(($1-2));yes \ |head -n$(($2*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
  n=$(($1-2))

  # The next couple of lines create a rectangle of spaces
  # matching the desired size
  yes ' '          |
  head -n$(($2*n)) |
  tr -d '\n'       |
  fold -w$n        |

  # Add the pipes and dashes
  sed '
    s/^\|$/|/g   # Replace first and last character by a pipe
    1! {$!b }    # Do nothing if not on first or last line
    s/ /-/g      # Add the dashes
  '
  echo
}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.

bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$(($1-2));yes \ |head -n$(($2*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.

bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$(($1-2));yes \ |head -n$(($2*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
  n=$(($1-2))

  # The next couple of lines create a rectangle of spaces
  # matching the desired size
  yes ' '          |
  head -n$(($2*n)) |
  tr -d '\n'       |
  fold -w$n        |

  # Add the pipes and dashes
  sed '
    s/^\|$/|/g   # Replace first and last character by a pipe
    1! {$!b }    # Do nothing if not on first or last line
    s/ /-/g      # Add the dashes
  '
  echo
}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.

1
source | link

bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$(($1-2));yes \ |head -n$(($2*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

You can now say f 4 3:

|--|
|  |
|--|

If you care about trailing new-line, add an echo at the end of function.